
该命令会输出执行的计划任务以及执行结果,帮助你判断自定义计划任务是否存在问题。
示例:
如果自定义的hits_set_zero任务没有被执行,或者执行结果不符合预期,则需要检查以下几个方面:
对于需要高度可靠的计划任务,建议使用Linux Cron替代WP-Cron。 Linux Cron是系统级别的任务调度器,可以按照预定的时间间隔精确地执行任务,不受网站访问量的影响。
配置方法:
示例:
假设要每分钟执行一次wp cron event run --due-now命令,可以添加以下行到Cron任务列表中:
* * * * * wp --path=/path/to/wordpress cron event run --due-now >/dev/null 2>&1
解释:
注意事项:
以下是原问题中的代码片段,并对其进行分析和改进:
add_action('woocommerce_share','setPostViews',70);
function setPostViews() {
global $product;
$product_id=$product->id;
$count_key = 'post_views_count';
$count = get_post_meta($product_id, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($product_id, $count_key);
add_post_meta($product_id, $count_key, '0');
}else{
$count++;
update_post_meta($product_id, $count_key, $count);
}
echo 'view::'.$count;
}
function hits_set_zero_schedule() {
if ( ! wp_next_scheduled( 'hits_set_to_zero') )
wp_schedule_event( time(), '10sec', 'hits_set_zero' );
}
add_action( 'wp', 'hits_set_zero_schedule' );
function hits_set_zero_func() {
global $product;
$product_id=$product->id;
delete_post_meta( $product_id, 'post_views_count', true );
}
add_action( 'hits_set_zero', 'hits_set_zero_func' );
function custom_cron_job_recurrence( $schedules )
{
if(!isset($schedules['10sec']))
{
$schedules['10sec'] = array(
'display' => __( 'Every 10 Seconds', 'twentyfifteen' ),
'interval' => 10,
);
}
if(!isset($schedules['15sec']))
{
$schedules['15sec'] = array(
'display' => __( 'Every 15 Seconds', 'twentyfifteen' ),
'interval' => 15,
);
}
return $schedules;
}
add_filter( ‘cron_schedules’, ‘custom_cron_job_recurrence’ );分析与改进:
改进后的代码:
// 增加产品浏览计数
function setPostViews() {
global $product;
if ( ! $product ) {
return; // 如果没有产品信息,则直接返回
}
$product_id = $product->get_id(); // 使用 get_id() 获取产品ID
$count_key = 'post_views_count';
$count = get_post_meta( $product_id, $count_key, true );
if ( $count == '' ) {
$count = 1; // 初始值为 1
update_post_meta( $product_id, $count_key, $count ); // 使用 update_post_meta 而不是 delete_post_meta + add_post_meta
} else {
$count++;
update_post_meta( $product_id, $count_key, $count );
}
return 'view::' . $count; // 返回浏览计数,而不是直接输出
}
add_action( 'woocommerce_share', 'setPostViews', 70 );
// 在模板中显示浏览计数
function displayPostViews() {
echo setPostViews();
}
add_action( 'woocommerce_single_product_summary', 'displayPostViews', 65 ); // 在产品摘要中显示
// 注册计划任务
function hits_set_zero_schedule() {
if ( ! wp_next_scheduled( 'hits_set_to_zero' ) ) {
wp_schedule_event( time(), 'hits_10sec', 'hits_set_zero' ); // 使用自定义的时间间隔 key
}
}
add_action( 'wp', 'hits_set_zero_schedule' );
// 重置浏览计数
function hits_set_zero_func() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // 获取所有产品
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID(); // 获取产品ID
delete_post_meta( $product_id, 'post_views_count' ); // 删除 post_views_count meta
}
wp_reset_postdata(); // 恢复原始 Post Data
}
}
add_action( 'hits_set_zero', 'hits_set_zero_func' );
// 定义自定义 Cron 时间间隔
function custom_cron_job_recurrence( $schedules ) {
if ( ! isset( $schedules['hits_10sec'] ) ) {
$schedules['hits_10sec'] = array(
'display' => __( 'Every 10 Seconds', 'twentyfifteen' ),
'interval' => 10,
);
}
return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_job_recurrence' ); // 注意单引号方向总结:
解决WordPress自定义计划任务不触发的问题,需要深入理解WP-Cron的工作原理和局限性。 通过使用WP-CLI进行调试,可以快速定位问题所在。 对于需要高度可靠的计划任务,建议使用Linux Cron替代WP-Cron。 同时,需要仔细检查代码,确保计划任务的注册、hook名称以及函数定义都正确无误。
以上就是WordPress自定义计划任务不触发问题排查与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号