Support

Account

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

  • Might could streamline the code a bit, but this is working for me. Hope it helps.

    // Expire events
    if ($expireTransient = get_transient($post->ID) === false) {
    	set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
    	$today = date('Y-m-d H:i:s', current_time('timestamp', 0));
    	$args = array(
    		'post_type' => 'events',
    		'posts_per_page' => 200,
    		'post_status' => 'publish',
    		'meta_query' => array(
    			array(
    				'key' => 'end_date_time',
    				'value' => $today,
    				'compare' => '<='
    			)
    		)
    	);
    	$posts = get_posts($args);
    	foreach( $posts as $post ) {
    		if(get_field('end_date_time', $post->ID)) {
    			$postdata = array(
    				'ID' => $post->ID,
    				'post_status' => 'draft'
    			);
    			wp_update_post($postdata);
    		}
    	}
    }