Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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