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?
error_logcall is displaying then the issue is not cron. When you say it doesn't go further thanmy_cron_jobthough, how are you testing that? Are you checking ifwp_insert_postsucceeded? I see you've usedarray($this, 'schedule_cron_job')butschedule_cron_jobandmy_cron_jobare bothstaticclass 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