—Resolved: Code Works as shown—
I have this piece of code that I think should work, but alas…
If anyone could take a peek and see if there something obvious. I have reviewed a lot of resources on the wordpress codex, but have tried all I can think of.
// expired_post_delete hook fires when the Cron is executed
add_action( 'expired_post_delete', 'delete_expired_posts' );
// This function will run once the 'expired_post_delete' is called
function delete_expired_posts() {
$todays_date = current_time('mysql');
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'date',
'meta_query' => array(
array(
'key' => 'date',
'value' => $todays_date,
'type' => 'DATE',
'compare' => '<'
)
)
);
$posts = new WP_Query( $args );
// The Loop
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post(get_the_ID());
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
// Add function to register event to WordPress init
add_action( 'init', 'register_daily_post_delete_event');
// Function which will register the event
function register_daily_post_delete_event() {
// Make sure this event hasn't been scheduled
if( !wp_next_scheduled( 'expired_post_delete' ) ) {
// Schedule the event
wp_schedule_event( time(), 'daily', 'expired_post_delete' );
}
}
the_date()
needs to be used within the Loop. Outside the Loop, it will return null. Use current_time() instead.