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

  • Just wanted to add that wstaley’s solution worked great for me, but you need to change his greater-than symbol to less-than. I also am not using the time of day, only the date, for expiration, so I changed time() to date().

    Here is my working code:

    // expire offer posts on date field.
    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'twicedaily', 'expire_posts'); // this can be hourly, twicedaily, or daily
    add_action('expire_posts', 'expire_posts_function');
    function expire_posts_function() {
    	$today = date('Ymd');
    	$args = array(
    		'post_type' => array('offer'), // post types you want to check
    		'posts_per_page' => -1 
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		$expiredate = get_field('valid_end_date', $p->ID, false, false); // get the raw date from the db
    		if ($expiredate) {
    			if($expiredate < $today){
    				$postdata = array(
    					'ID' => $p->ID,
    					'post_status' => 'draft'