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);
}
}
}
}