WordPress: feature that drops posts after 2 months
I've created this little function that will draft all posts older than 2 months. The function need to be placed in your theme's functions.php file and can be run via a wp-cron or manually via a get function.
// Helper function
function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A');
$current_post['post_status'] = $status;
wp_update_post($current_post);
}
// Update function
function putoldpostsasdraft() {
$posts = get_posts(array( 'post_type' => 'post', 'posts_per_page' => - 1, 'date_query' => array( 'column' => 'post_date_gmt', 'before' => '2 month ago')) ) );
foreach ($posts as $post) {
$id = $post->ID;
change_post_status($id, 'draft');
var_dump('drafted: '. $id);
}
}
WordPress post statuses:
If you want to change the post status to something other than draft, I have listed a number of WordPress post statuses that can be used, in addition to "draft":
publish|pending|draft|private|static|object|attachment|inherit|future|trash