0

I have a cron job set up with the following code in my functions.php file

I have a CPT with a custom field that defines an expiration date for each post. All I am doing is checking if the date has past compared to the current date and running it daily.

The function defined below, opportunities_cron_job_fun() runs as expected when called manually in functions.php, however when the cron job runs it doesn't work.

Below is the function I need to run.

function opportunities_cron_job_func() {

    $args = array(
        'post_type' => 'opportunities', 
        'posts_per_page' => -1,
    );

    $posts = get_posts($args);

    foreach ($posts as $post) {

        $date_field_value = get_field('listing_expiry', $post->ID);

        error_log($date_field_value);

        if ($date_field_value && strtotime($date_field_value) < time()) {
            // Remove by setting to draft - let deletion be up to site managers
            $updated = wp_update_post(array(
                'ID' => $post->ID,
                'post_status' => 'draft',
            ));
            if (is_wp_error($updated)) {
                error_log('Error updating post: ' . $updated->get_error_message());
            }
        }
    }
}
add_action('opportunities_cron_job', 'opportunities_cron_job_func');

Below is how I am scheduling it, the site gets alot of traffic and I am not worried about it not getting triggered.

// schedule cron job 
function schedule_opportunities_cron() {
    if (!wp_next_scheduled('opportunities_cron_job')) {
        wp_schedule_event(time(), 'daily', 'opportunities_cron_job');
    }
}
add_action('init', 'schedule_opportunities_cron');

I am also seeing some weird behavior when viewing the cron job via the WP Control plugin.

The "next run" column is 53 years in the past and the recurrence column says "non-repeating" which makes zero sense and I cannot figure out what's going on there....

WP Control columns that make no sense

The positive note in the screenshot is that the hook appears to be attached to the correct action... even though it doesn't seem to be actually running...

One final note, this is a Multi-site I am not sure if I need to alter my code to accomodate this... I am inclined to think this is not the case as calling the opportunitites_cron_job_func() works...

Any help is appreciated.

2 Answers 2

1

It looks like the time() was not set correctly in the first iteration of the function wp_schedule_event;

  1. First, remove this task from the queue by adding a function wp_clear_scheduled_hook and reload the page. For your example: wp_clear_scheduled_hook( 'opportunities_cron_job' );
  2. Remove the cleaning function.
  3. Restart your code. If your date is not set correctly again, use the time in UNIX format, for example, ʼ1701009175ʼ and repeat all actions again.
1
  • This did correct the issue with the timestamp in that the "next run" is no longer in the past and the "non-repeating" changed to "daily" as expected. using that function to remove the cron job was a big help, ultimately the issue was due to the development server being behind a basic auth which was preventing the cron from running... there was also an issue with the conversion of an ACF to UNIX not working right in this line: if ($date_field_value && strtotime($date_field_value) < time()) but I got it working! Thanks for the input. Dec 6, 2023 at 20:43
0

DMTlo posted a helpful bit of insight that helped me to get on the right track and get the cron properly set up and remove old instances of the cron with wp_clear_scheduled_hook as I was testing.

Ultimately I discovered the cron was not running because the development site was behind a Basic Auth wall and that was preventing the cron from ever being triggered to run.

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.