Skip to content

Instantly share code, notes, and snippets.

@ramseyp
Created November 3, 2011 03:59
Show Gist options
  • Save ramseyp/1335744 to your computer and use it in GitHub Desktop.
Save ramseyp/1335744 to your computer and use it in GitHub Desktop.
Set Posts older than 3 years from current date to draft status
<?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