4

I created a plugin with a cron to update all posts of a certain type every 5 minutes. I installed WP Crontrol to check if the cron is registered correctly and everything seems to be okay.

This is how I registered my cron:

function custom_cron_interval( $schedules ) {
    $schedules['fiveminutes'] = array(
        'interval' => 300,
        'display' => __('Every five minutes')
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_interval' ); 

if ( ! wp_next_scheduled( 'recalculate_all_scores' ) ) {
    wp_schedule_event(time(), 'fiveminutes', 'recalculate_all_scores');
}

It is registered correctly but when the function executes, nothing happens. For testing purposes I hooked the function to the save_post action. Everything works fine this way. But when the scheduled task is called, it won't execute.

Here is the code of the function

function recalculate_all_scores() {
    global $wpdb; 

    $customers = $wpdb->get_results("SELECT * FROM wp_posts 
                                WHERE post_type = 'customer' 
                                AND post_status = 'publish';");

    foreach ($customers as $customer){
        set_score($customer);
    }
}

function set_score($customer){
    $acf_key = "score";
    $score = rand(0,50);
    update_field( $acf_key, $score, $customer->ID );
}

I also added the following lines to my wp-config.php:

define('ALTERNATE_WP_CRON', true);
define('DISABLE_WP_CRON', false);

Any idea what is stopping the function from executing?

EDIT

  • I'm aware that the wp_cron system is not a real cron and that it relies on site visits.
  • If I execute the function manually it takes about 15 seconds so the PHP execution time limit should not be a problem.
2
  • Are you aware the wp_cron is not a true cron system and infact relies on site visits to kick off? Also are you aware that there are execution times in your PHP settings which could limit execution? i.e. A task execution limit may be set to 3 minutes in your PHP ini and if your task is waiting 5 minutes the execution may be getting cancelled Jan 22, 2015 at 9:52
  • @CraigPearson I'm aware of that and I updated my question. Thanks for the reply! Jan 22, 2015 at 10:28

2 Answers 2

0

I was having the same issue recently, until I followed an example from the Wordpress Codex which suggests using action hooks to run the function.

I think if you add the following...

add_action( 'recalculate_all_scores_hook', 'recalculate_all_scores' );

...and amend the wp_schedule_event function to use the action hook name rather than the function directly...

wp_schedule_event( time(), 'fiveminutes', 'recalculate_all_scores_hook' );

...you might be in business.

Good luck!

1
  • I tried this before but it didn't change a thing... In the end I solved it by hooking it in to an existing event. Feb 16, 2015 at 9:27
-2

WordPress only support hourly, daily and twise daily, so change five minutes with any of three and set your function with sleep(10); or any other time which suitable for your plugin

Check here

1
  • That is not true, you can define, as the OP is doing, custom time intervals with the cron_schedules filter. hourly, daily and twise daily are the intervals defined in the core but WordPress supports any desired interval.
    – cybmeta
    Nov 16, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.