OK, I've got this working with some more research on this forum. Here's my code:
// expire exams on date field.
if (!wp_next_scheduled('expire_exams')){
wp_schedule_event(time(), 'hourly', 'expire_exams'); // this can be hourly, twicedaily, or daily
}
add_action('expire_exams', 'expire_exams_function');
function expire_exams_function() {
$today = date('Ymd');
$args = array(
'post_type' => array('exam'), // post types you want to check
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach($posts as $p){
$expiredate = get_post_meta($p->ID, 'end_date', true ); // get the raw date from the db
if ($expiredate) {
if($expiredate < $today){
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
}
}
}
I then used the WP Crontrol plugin to get more visibilty over the cron job.