按日期元数据自定义字段删除Wordpress中的过期帖子 [英] Delete expired posts in wordpress by date meta data custom field
本文介绍了按日期元数据自定义字段删除Wordpress中的过期帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
---已解决:代码显示如下---
---Resolved: Code Works as shown---
我有一段我认为应该可以运行的代码,但是a ...
I have this piece of code that I think should work, but alas...
如果有人可以窥视一下,看看是否有明显的东西。我已经回顾了Wordpress Codex上的大量资源,但尝试了所有我能想到的。
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()
需要在循环中使用。在循环外部,它将返回null。改为使用 current_time()。
the_date()
needs to be used within the Loop. Outside the Loop, it will return null. Use current_time() instead.
$todays_date = current_time('mysql'); // returns YYYY-MM-DD HH:MM:SS
这篇关于按日期元数据自定义字段删除Wordpress中的过期帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文