Wordpress cron在不正确的时间间隔上运行


Wordpress cron running on incorrect time intervals

出于测试目的,我为cron保留较短的时间间隔,当功能正常工作时,我将其更改为所需的时间间隔。每当我将ex的时间间隔从"three_days"更改为"five_minutes"或从"five_minutes"更改为‘fiften_minutes’时,cron都会使用较早设置的时间间隔运行,而不是更新的时间间隔。我对此完全感到困惑。

这可能是什么原因,请帮我解决这个问题。

这是我的代码:

add_filter('cron_schedules', 'filter_cron_schedules');
function filter_cron_schedules($schedules) {
    $schedules['fifteen_minutes'] = array(
        'interval' => 900, // seconds
        'display'  => __('Every 15 minutes') 
    );
    $schedules['twenty_minutes'] = array(
        'interval' => 1200, // seconds
        'display'  => __('Every 20 minutes') 
    );
    $schedules['three_days'] = array(
        'interval' => 259200, // seconds
        'display'  => __('Every 3 days') 
    );
    $schedules['five_minutes'] = array(
        'interval' => 300, // seconds
        'display'  => __('Every 5 minutes') 
    );
    return $schedules;
}
// Schedule the cron
add_action('wp', 'bd_cron_activation');
function bd_cron_activation() {
    if (!wp_next_scheduled('bd_cron_cache')) {
        wp_schedule_event(time(), 'twenty_minutes', 'bd_cron_cache'); // hourly, daily, twicedaily
    }
}
// Firing the function
add_action('bd_cron_cache', 'bd_data');
function bd_data() {
    // My Logic
}

以下是该问题的修复方法:之前在//调度我的代码的cron部分,我检查是否没有调度cron,然后重新调度cron。由于我的cron已经设置好了,所以条件返回false,并且没有设置新的间隔。

// Schedule the cron
add_action('wp', 'bd_cron_activation');
function bd_cron_activation() {
    if (!wp_next_scheduled('bd_cron_cache')) {
        wp_schedule_event(time(), 'twenty_minutes', 'bd_cron_cache'); // hourly, daily, twicedaily
    }
}

解决方法是首先检查活动是否按您的时间间隔安排,如果没有,则从以前的时间间隔取消计划,并按我们的新时间间隔重新安排。以下是相同的代码:

add_filter('cron_schedules', 'filter_cron_schedules');
function filter_cron_schedules($schedules) {
    $schedules['fifteen_minutes'] = array(
        'interval' => 900, // seconds
        'display'  => __('Every 15 minutes') 
    );
    $schedules['twenty_minutes'] = array(
        'interval' => 1200, // seconds
        'display'  => __('Every 20 minutes') 
    );
    $schedules['three_days'] = array(
        'interval' => 259200, // seconds
        'display'  => __('Every 3 days') 
    );
    $schedules['five_minutes'] = array(
        'interval' => 300, // seconds
        'display'  => __('Every 5 minutes') 
    );
    return $schedules;
}
// Schedule the cron
add_action('wp', 'bd_cron_activation');
function bd_cron_activation() {
    if ( wp_get_schedule('bd_cron_cache') !== 'two_minutes' ) {
            // Above statement will also be true if NO schedule exists, so here we check and unschedule if required
            if ( $time = wp_next_scheduled('bd_cron_cache'))// Get Previously scheduled time interval
                wp_unschedule_event($time, 'bd_cron_cache'); // Unschedule the event for that time interval
            wp_schedule_event(time(),'two_minutes','bd_cron_cache'); // Scheduling out event with new time interval.  
        }
}
// Firing the function
add_action('bd_cron_cache', 'bd_data');
function bd_data() {
    // My Logic
} 

此修复程序的核心逻辑由@TheDeadMedic(wordpress stackexchange成员)回答。