我使用wp_schedule_event()来安排一个任务在我的网站上每小时运行一次。由于某种原因,我想运行的函数工作正常,事件本身出现在“cron”选项表下。一切看起来都很好,但是当我等待并检查函数是否运行时,我什么也没有。
我每小时使用一次测试,但它意味着每天
下面是php代码的完整片段:
/*
======================================================
MANAGE SUB CANCELLATIONS
1 - daily event to run
2 - manage_sub_cancel()
======================================================
*/
/* *
*
* -- > 1 - daily event to run
*
* */
add_action( 'wp', 'check_sub_activation' );
function check_sub_activation() {
if ( ! wp_next_scheduled( 'check_sub_event' )) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'check_sub_event' ); // creating the hook
}
}
// add_action( 'wp', 'check_sub_deactivation');
function check_sub_deactivation() { // will clear the scheduled hook if activated
wp_clear_scheduled_hook('check_sub_event');
}
/* *
*
* -- > 2 - manage_sub_cancel()
*
* */
function manage_sub_cancel() { // This function will apply the cancellation of a boutique when period end is reached
$args = array(
'post_type' => 'boutique',
'meta_key' => 'cancel_at_end',
'meta_value' => 'true',
'meta_compare' => '=',
);
$the_query = new WP_Query( $args );
$boutique_list = $the_query->posts;
foreach ( $boutique_list as $boutique ) {
$boutique_id = $boutique->ID;
$period_end = get_post_meta( $boutique_id, 'period_end', true );
$today = time();
if ( $today < $period_end ) { // À INVERSER LORSQUE TERMINÉ
update_post_meta( $boutique_id, 'sub_id', "" );
update_post_meta( $boutique_id, 'pckg_id', "1" );
update_post_meta( $boutique_id, 'cancel_at_end', "false" );
update_post_meta( $boutique_id, 'period_end', "" );
} else {
// Period end is not passed
}
}
}
add_action( 'check_sub_event', 'manage_sub_cancel' ); // add manage_sub_cancel() function to 'check_sub_event' hook we just created
我也在使用插件高级密码管理器来列出所有的Cron作业,我看到了我在这里创建的任务,我也可以“立即执行”,它正在工作。但是,当我等待偶数运行每小时,我没有看到任何东西,即使我访问网站。我不知道似乎一切都做得很好,但什么也没发生。
发布于 2019-11-18 19:55:24
WordPress cron作业并不是实际的cron作业。WordPress将在下一次执行脚本时检查作业是否已经运行,即当用户访问导致脚本运行的页面时,然后在需要时运行cron作业。如果没有人加载网页,那么脚本将不会运行,因此您的cron作业也不会运行。
您只有几个选项可以用来绕过这个限制:
root
访问权限,那么设置您自己的个人服务器,然后在这个新服务器上执行#1操作。这些都是按优先顺序列出的。第一是最好的,第二是更昂贵和稍微复杂,但仍然是一个有点合理的选择,第三是相当可怕和不可靠的,但可能会在紧要关头。
https://stackoverflow.com/questions/58921784
复制相似问题