Set post to draft based on ACF date field

Hi team, hopefully you can help with my issue. What am I missing?:


I have a custom post type called “member”.

I have a custom field called “expiry_date”.

Im trying to have a member post put automatically into draft mode when the expiry_date occurs.


I have created a Cron which seems to work but the post is not going into draft.

The function:

// expire offer posts on date field.

if (!wp_next_scheduled('expire_posts')){

	wp_schedule_event(date(), 'daily', '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('member'), // post types you want to check

	'posts_per_page' => -1

	);

	$posts = get_posts($args);

	foreach($posts as $p){

		$expiredate = get_field('expiry_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'

				);

				wp_update_post($postdata);

			}

		}

	}

}

Hello @kellybaya,

Thanks for writing in! Be advised that custom PHP coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.