我有一个挂钩到save\\u post的函数,它根据今天的日期更改自定义分类术语的值。这很好用。当我更新帖子时,分类术语会正确更新。
但我希望每天自动更新一次,而不必自己手动更新帖子。我阅读了wp\\u cron并创建了下面的代码,这基本上是一种尝试,每天查询一组特定的帖子,然后通过wp\\u update\\u post()简单地运行它们,这将触发上面提到的更改分类法术语值的函数。
但它似乎不起作用。此外,我真的不知道如何测试wp\\u cron,而不只是等待一天,看看会发生什么。我安装了一个控制台,这样我就可以看到即将到来的预定事件,下面的事件是明确安排的,似乎没有任何效果。
有谁能就我可能出了什么问题提供一些建议吗?
//Schedule active events to update_post every day
if( !wp_next_scheduled( \'event_status_refresh\' ) ) {
wp_schedule_event( time(), \'daily\', \'event_status_refresh\' );
}
//When the event fires, we call the function to update the posts
add_action( \'event_status_refresh\', \'update_event_status\' );
//The function to update the posts
function update_event_status() {
global $post;
$active = array(
\'post_type\'=>\'event\',
\'posts_per_page\'=>\'-1\',
\'tax_query\'=>array(
array(
\'taxonomy\'=>\'event_status\',
\'field\'=>\'slug\',
\'terms\'=>array(
\'playing_now\',
\'opening_soon\'
)
)
),
);
$query = new WP_Query($active);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$update = array(
\'ID\' => get_the_ID(),
);
// Update the post into the database
wp_update_post( $update );
}
}
wp_reset_postdata();
}