WordPress runs its own cron events by default. We can hook into this to run our own cron scripts, a useful one of which is to schedule some posts to revert to draft status.
/** * Add a new time to the Cron Scheduler */ add_filter('cron_schedules', 'cron_add_schedules'); function cron_add_schedules($schedules) { # Adds once weekly to the existing schedules. $schedules['weekly'] = array( 'interval' => 604800, 'display' => __('Once Weekly') ); # Adds 5minutes to the existing schedules $schedules['every_five_minutes'] = array( 'interval' => 300, 'display' => __('Every 5 Minutes') ); # Add every minute schedule $schedules['every_minute'] = array( 'interval' => 60, 'display' => __('Every Minute') ); return $schedules; } /** * Add a scheduled event */ if(!wp_next_scheduled('draft_ old_posts')) { wp_schedule_event(time(), 'every_minute', 'draft_old_posts'); } /** * This function takes published posts and changes * them to drafts */ function draft_old_posts_function() { $query = new WP_Query(array( 'post_type' => 'post', 'post_status' => 'any' )); if($query->have_posts()) { while($query->have_posts()) { $query->the_post(); $current_post = get_post($post->ID, 'ARRAY_A'); $current_post['post_status'] = 'draft'; wp_update_post($current_post); } } } add_action('draft_old_posts', 'draft_old_posts_function'); /** * Unschedule an Event */ if(false !== ($time = wp_next_scheduled('draft_old_ posts'))) { wp_unschedule_event($time, 'draft_old_posts'); }