Home Forums ACF PRO Expire Posts on Datepicker Field Reply To: Expire Posts on Datepicker Field

  • Hi @karks88

    But I would say there’s no need for you to query all posts and then do the check when you can just query all past posts directly and change their statuses..

    if (!wp_next_scheduled('expire_posts')){
    	wp_schedule_event(time(), 'hourly', 'expire_posts'); // running hourly for testing purposes
    add_action('expire_posts', 'expire_posts_function');
    function expire_posts_function() {
    	$today = time('Ymd'); // matches the date in DB
    	$args = array(
    		'post_type' => array('post'), // array of the post types you want to check
    		'posts_per_page' => 200 // get all the posts but don't set it t o -1 since its a risk of overloading the server
    		'category' => 'events',
    		'post_status' => 'publish',
    		'meta_query' => array(
    				'key' => 'event_ending_date',
    				'value' => $today,
    				'compare' => '<' // you might have to switch this out for >
    	$post_query = new WP_Query($args);
    	if( $posts->have_posts() ){
    		while( $post_query->have_posts() ){
    			wp_transition_post_status('draft', 'publish', $post);