Hourly scheduled wp_cron job keeps getting rescheduled

Every time I refresh the page, my cron job update_backup_now_feed seems to have its timer reset. I’m using a plugin called “crontrol” that displays the time until next call, and every time I refresh it resets to 1 hour. The only time the schedule seems to fire off is when I avoid the site for over an hour and then refresh.

Am I doing something wrong with my code below or is this a bug? I’m working on Multisite. I found this question which is similar – and in the comments section of the answer, the user mentioned having the same problems only on multisite.

Why does refreshing the page reset the timer for wp cron events?

Here is all of my cron related code.

// add custom cron interval
add_filter('cron_schedules', 'add_custom_cron_intervals', 10, 1);
function add_custom_cron_intervals($schedules) {
    $schedules['every_two_minutes'] = array(
    'interval'  => 120,
    'display' => 'Every Two Minutes'
    );

    return (array)$schedules; 
}

add_action('init', 'register_update_feed_event');
function register_update_feed_event() {
  if(!wp_next_scheduled('update_now_feed')) {
        wp_schedule_event(time(), 'every_two_minutes', 'update_now_feed');
    }

    if(!wp_next_scheduled('update_backup_now_feed')) {
        wp_schedule_event(time(), 'hourly', 'update_backup_now_feed');
    }
}   

add_action('update_now_feed', 'update_now_feed_function');
function update_now_feed_function() {
    NowFeed::set_instagram_cache();
    NowFeed::set_twitter_cache();
}


add_action('update_backup_now_feed', 'update_backup_now_feed_function');
function update_backup_now_feed_function() {
    NowFeed::set_instagram_backup_cache();
    NowFeed::set_twitter_backup_cache();
}

UDPATE
I thought I fixed it, but instead it raises more questions.

For some portion of last night, my code looked like this below. I had removed the wp_next_scheduled check… and it literally created 50 different scheduled events.

//if(!wp_next_scheduled('update_backup_now_feed')) {
    wp_schedule_event(time(), 'hourly', 'update_backup_now_feed');
//}

The crontrol only showed one of them, since they all had the same name. I realized I could keep deleting them, and they didn’t go away. I kept clicking and finally, after ~50 clicks they were gone. This definitely helped some of the issues I was having. Don’t pull a me and remove that conditional (although I’m not sure how you update cron jobs otherwise.. )

I have now put this code in my WP_Config. define('DISABLE_WP_CRON', true); to see if I could troubleshoot more.

At this point, refreshing the page would NOT call any of my events. However, I went to the custom URL to test cron – /wp-cron.php?doing_wp_cron and when I refresh it, the response from my action sometimes shows. This is the part I don’t understand.

Does disabled wp cron and enabling it via real cron cause the ‘timers’ to stop functioning as expected? If I have an hourly job and I call the cron every 5 minutes, will it also be called every 5 minutes? Why does calling it manually sometimes call the event and sometimes not?

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Hourly scheduled wp_cron job keeps getting rescheduled

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.