这个Cron有什么问题?

时间:2018-12-04 18:55:22

标签: php wordpress cron

此代码是否有问题? 它不起作用。 在wp-config中,我将DISABLE_WP_CRON设置为false。 我不知道怎么了。 SQL查询工作正常。它应该每周和一次重新设置所有帖子的元值。为了测试目的,我给了10到30秒,但是就像我说的那样-它不起作用。有人可以帮忙吗?

预先感谢;)

<pre><code>
function os_custom_cron_schedule( $schedules ) {
    $schedules['ten_seconds'] = array(
        'interval' => 10, //60 * 60* 24 * 7, // Every week
        'display'  => __( 'Every week' ),
    );
    $schedules['30_seconds'] = array(
        'interval' => 30, //60 * 60* 24 * 30, // Every month
        'display'  => __( 'Every month' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'os_custom_cron_schedule', 10, 1 );

//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'os_cron_hook_week' ) ) {
    wp_schedule_event( time(), 'ten_seconds', 'os_cron_hook_week' );
}
if ( ! wp_next_scheduled( 'os_cron_hook_month' ) ) {
    wp_schedule_event( time(), '30_seconds', 'os_cron_hook_month' );
}

///Hook into that action that'll fire in given time
add_action( 'os_cron_hook_week', 'week_reset_views' );
add_action( 'os_cron_hook_month', 'month_reset_views' );

//create your function, that runs on cron
function week_reset_views() {
    global $wpdb;
    $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = 'post_views_weekly'");
}
function month_reset_views() {
    global $wpdb;
    $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = 'post_views_monthly'");
}
</pre></code>

0 个答案:

没有答案