wp_schedule_event 已计划但功能未触发
wp_schedule_event is planned but function not firing
我在使用 wp_schedule_event 函数时遇到了一些问题,到目前为止我无法在这里找到任何解决方案。
事件已触发,至少我在调用 wp_next_scheduled( 'update_expired_events_hourly' )
时看到了下一个计划,但是当时间到了时,什么也没有发生。
当我手动调用它时,该功能也能正常工作。我试图通过调用 /wp-cron.php?doing_wp_cron 来触发事件,但这也没有效果。
以下代码在我的主题中functions.php
有什么解决方法的建议吗?
提前致谢!
add_action( 'wp', 'expired_activation' );
add_action( 'update_expired_events_hourly', 'update_expired_events' );
function expired_activation(){
if ( !wp_next_scheduled( 'update_expired_events_hourly' ) ) {
wp_schedule_event(time(), 'hourly', 'update_expired_events_hourly');
}
}
function update_expired_events(){
// events query
$args = array(
'post_type' => 'espresso_events',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
'post_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'espresso_event_categories',
'field' => 'slug',
'terms' => 'ee_exp', // the expired category slug
'operator' => 'NOT IN',
),
)
);
$events = new WP_QUERY($args);
$today = strtotime(current_time( "Y-m-d H:i:s", $gmt =0 )); // get current date
if ( $events->have_posts() ){
while ( $events->have_posts() ){
$events->the_post();
//get event end date
$event_end = strtotime( espresso_event_end_date( 'Y-m-d', 'H:i:s', false, false ) ); // get event end date
if( $event_end - (60*60*24) < $today ){
wp_set_object_terms( get_the_ID(), 'ee_exp', 'espresso_event_categories', true); // Add post to expired category
}
}
}
}
我忘了说,那个定义('DISABLE_WP_CRON');在我的 config.php
中设置为 false
--------------------编辑--------------------
上面的代码正在运行。显然,唯一不起作用的是通过 URL (/wp-cron.php?doing_wp_cron) 触发事件。对此深表歉意,感谢您的帮助!
我认为你不应该禁用 wp_cron 我建议你使用 wp_schedule_single_event 它对我来说效果很好
wp_schedule_single_event( time() + 3600, 'update_expired_events_hourly' );
我在使用 wp_schedule_event 函数时遇到了一些问题,到目前为止我无法在这里找到任何解决方案。
事件已触发,至少我在调用 wp_next_scheduled( 'update_expired_events_hourly' )
时看到了下一个计划,但是当时间到了时,什么也没有发生。
当我手动调用它时,该功能也能正常工作。我试图通过调用 /wp-cron.php?doing_wp_cron 来触发事件,但这也没有效果。
以下代码在我的主题中functions.php
有什么解决方法的建议吗?
提前致谢!
add_action( 'wp', 'expired_activation' );
add_action( 'update_expired_events_hourly', 'update_expired_events' );
function expired_activation(){
if ( !wp_next_scheduled( 'update_expired_events_hourly' ) ) {
wp_schedule_event(time(), 'hourly', 'update_expired_events_hourly');
}
}
function update_expired_events(){
// events query
$args = array(
'post_type' => 'espresso_events',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
'post_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'espresso_event_categories',
'field' => 'slug',
'terms' => 'ee_exp', // the expired category slug
'operator' => 'NOT IN',
),
)
);
$events = new WP_QUERY($args);
$today = strtotime(current_time( "Y-m-d H:i:s", $gmt =0 )); // get current date
if ( $events->have_posts() ){
while ( $events->have_posts() ){
$events->the_post();
//get event end date
$event_end = strtotime( espresso_event_end_date( 'Y-m-d', 'H:i:s', false, false ) ); // get event end date
if( $event_end - (60*60*24) < $today ){
wp_set_object_terms( get_the_ID(), 'ee_exp', 'espresso_event_categories', true); // Add post to expired category
}
}
}
}
我忘了说,那个定义('DISABLE_WP_CRON');在我的 config.php
中设置为 false--------------------编辑-------------------- 上面的代码正在运行。显然,唯一不起作用的是通过 URL (/wp-cron.php?doing_wp_cron) 触发事件。对此深表歉意,感谢您的帮助!
我认为你不应该禁用 wp_cron 我建议你使用 wp_schedule_single_event 它对我来说效果很好
wp_schedule_single_event( time() + 3600, 'update_expired_events_hourly' );