Created
November 3, 2011 03:59
-
-
Save ramseyp/1335744 to your computer and use it in GitHub Desktop.
Set Posts older than 3 years from current date to draft status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set Posts older than 3 years from current date to draft status | |
* @link https://gist.github.com/ | |
* @param string | |
* @return string | |
*/ | |
add_action( 'init', 's25_mass_expire' ); | |
function s25_mass_expire(){ | |
global $wpdb; | |
//get the current date as year | |
$server_time_yr = date('Y'); | |
//set our cutoff to 3 years back from current date as year | |
$cutoffyr = date( 'Y' ,strtotime ( '-3 year' , strtotime($server_time_yr) ) ); | |
//select all published Posts | |
$result = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish'"); | |
//go through each returned Post, if the Post's year is older than the cutoff, set the status of the Post to Draft | |
if( !empty($result)) { | |
foreach ($result as $a){ | |
$show_time = get_the_time('Y', $a->ID ); | |
var_export($show_time); | |
if ( $cutoffyr > $show_time){ | |
$my_post = array(); | |
$my_post['ID'] = $a->ID; | |
$my_post['post_status'] = 'draft'; | |
wp_update_post( $my_post ); | |
} | |
} // end foreach | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment