WP Cron scheduling doesn’t work reliably on hosted server


The following code is part of a save_post action. It works as expected in my local dev environment, but doesn’t work reliably on a hosted server. (By unreliably I mean it sometimes work, but without any regularity I can deduce.)

if (wp_next_scheduled('my_expiration_hook', [$post_id])) {
    my_log('unscheduling all for post ' . $post_id);
    wp_clear_scheduled_hook('my_expiration_hook', [$post_id]);

if ($today < $expire) {
    my_log('scheduling ' . $post_id . ' with date ' . $expire);
    wp_schedule_single_event(strtotime($expire), 'my_expiration_hook', [$post_id]);

Everything logs correctly, but it is as if the wp_clear_scheduled_hook and wp_schedule_single_event calls never occur.

I am using WP Crontrol plugin to check the schedule. Manually modifying the schedule with this plugin works as expected.

Am I missing something or is this a hosting issue? Thanks!

noos 1 year 2022-01-26T16:02:28-05:00 0 Answers 0 views 0

Leave an answer