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....
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.
