0

I'm trying to run a WP cron task. This is my code

//constructor add_action('my_custom_event_hook', array( $this, 'my_cron_job' )); add_action('init', array($this, 'schedule_cron_job')); public static function schedule_cron_job() { $timestamp = time(); $recurrence = 'every_minute'; error_log('schedule_cron_job'); wp_schedule_event(time(), 'every_minute', 'my_custom_event_hook'); } public static function my_cron_job() { error_log('my_cron_job'); $post_data = array( 'post_title' => 'My Scheduled Post', 'post_content' => 'This is a scheduled post created by a cron job.', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'post' ); wp_insert_post($post_data); } 

it only prints the schedule_cron_job method. but does not go any further than that. What am I doing wrong?

1
  • If your first error_log call is displaying then the issue is not cron. When you say it doesn't go further than my_cron_job though, how are you testing that? Are you checking if wp_insert_post succeeded? I see you've used array($this, 'schedule_cron_job') but schedule_cron_job and my_cron_job are both static class methods which would mean your PHP error log is filling up with errors and warnings. Normally for static methods you would use [ 'NameOfClass', 'methodName' ] instead of [ $this, 'methodName' ], though I see no reason for the methods to be static here Commented Jun 6, 2024 at 9:49

2 Answers 2

2

There are a number of issues here:

  1. every_minute doesn't exist as a cron schedule, so while you've successfully scheduled the cron job, WordPress doesn't know when to run it.
  2. The code binds that the class method/functions to the hook dynamically, but those are static methods, either remove the static keyword or use the class name instead of $this
  3. You're assuming that wp_insert_post always works, it can fail! It returns a post ID or a failure value, assign it and check that value so you can report that something went wrong

Finally, WP Cron issues can also be debugged using WP CLI or plugins such as wP Crontrol that let you view what has been scheduled, when, and if they're overdue.

1

It looks like your issue might be related to the custom recurrence schedule 'every_minute' that isn't defined by default in WordPress. WordPress has standard schedules like 'hourly', 'twicedaily', and 'daily', but 'every_minute' isn’t included in these defaults.

To fix this, you need to define your custom interval for 'every_minute'. Here's how you can add it:

add_filter('cron_schedules', 'add_custom_cron_interval'); function add_custom_cron_interval($schedules) { $schedules['every_minute'] = array( 'interval' => 60, // Every 60 seconds 'display' => esc_html__('Every Minute'), // Display name for the interval ); return $schedules; } 

Include this in your theme’s functions.php file or in the appropriate place in your plugin. After this, your schedule for 'every_minute' should work as expected, and the cron job will trigger your my_cron_job function every minute.

1
  • And to schedule it daily. Is this the correct way? public static function schedule_custom_cron_task() { if (!wp_next_scheduled('custom_cron_task_event')) { wp_schedule_event(time(), 'daily', 'custom_cron_task_event'); } } public static function custom_cron_schedules($schedules) { $schedules['daily'] = array( 'interval' => (60 * 60 * 24), 'display' => __('Every Day') ); return $schedules; } Commented Jun 11, 2024 at 7:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.