Support

Account

Home Forums General Issues Expire post to draft Reply To: Expire post to draft

  • You could just create a function that loops through all your posts and checks the time, and if expired updates the post status to draft. Run it on cron to keep it from killing performance.

    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'daily', 'expire_posts');
    }
    
    add_action('expire_posts', 'expire_posts_function');
    
    function expire_posts_function() {
    	$today = time();
    	$args = array(
    		'post_type' => array('post'), // array of the post types you want to check
    		'posts_per_page' => -1 // get all the posts
    	);
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		if(get_field('expiration', $p->ID) > $today){
    			$postdata = array(
    				'ID' => $p->ID,
    				'post_status' => 'draft'
    			);
    			wp_update_post($postdata);
    		}
    	}
    }