Search code examples
phpwordpresscron

Wordpress Cron Job is not running


I set up a Wordpress Cron Job to run every 5 minutes and execute a function called bulk_update(). When I run the function manually through the admin, it works.

However, it seems the Cron job is not executing the function. I added error_log(print_r("EXECUTION OF BULK_UPDATE"));to the beginning of the function in order to know if the Cron job is successfully calling the function, but I don't get anything in the logs.

Here is the bulk_update function:

function bulk_update() {

    error_log(print_r("LAUNCH OF BULK_UPDATE")); 

    $args = array(
            'post_type' => 'page',
            'date_query' => array(
                array(
                    'column' => 'post_modified_gmt',
                    'before'  => '2019-02-17 23:59:59',
                     ),
                ),
            'posts_per_page' => 20,
             );

    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {

        echo $the_query->found_posts . "<br />";

        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $post_id = get_the_ID();
            $slug = get_post_field( 'post_name', $post_id );
            $key =  explode("n-", $slug);
            echo $key[1] . "<br/>";
            update_dico_page($key[1], $post_id);
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        echo "No post found.";
    }
}

I have other Cron jobs with a similar setup and it works perfectly. I use WP Crontrol to manage my Cron jobs and the setup seems rights:

enter image description here

Any idea what could prevent this cron job to work?


Solution

  • I solved the problem by creating a server cron job. Apparently the issue was with the Wordpress Cron system.