---已解决:代码如图所示---
我认为这段代码应该可行,但唉......
如果有人可以偷看,看看是否有明显的东西。我已经回顾了很多关于wordpress codex的资源,但我已经尝试了所有我能想到的。
// 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' );
}
}
答案 0 :(得分:0)
the_date()
。在循环之外,它将返回null。请改用current_time()。
$todays_date = current_time('mysql'); // returns YYYY-MM-DD HH:MM:SS