advanced custom fields – WP Cron Working, but Function Not Working

Question

I have a plugin I built for events. I set up the plugin with WP Cron to run a function. The function checks the events posts to see if it’s recurring then if the start date has passed it updates the start date to the next day of the week that event will occur on. This function works with a button I coded in the admin that calls the function with admin ajax, so I know the function works. However, when I run this function with the cron the function is not working. I have this set up also on cron-job.org to ping the site so that the function runs. I can also see in WP Control that the cron is firing and the function is listed under the action column and it’s not erroring out. Below is my code. Could anyone help me figure out what I am doing wrong? P.S. I changed the time interval from daily to hourly for testing.

/**
 * SETUP CRON JOB TO RUN DAILY AND FIRE EVENT CHECK FUNCTION
 */
register_activation_hook(__FILE__, 'fep_event_check_schedule');

function fep_event_check_schedule() {

    $timestamp = wp_next_scheduled('fep_event_check_hourly');

    if(!$timestamp) {
        wp_schedule_event(time(), 'hourly', 'fep_event_check_hourly');
    }

}

add_action( 'fep_event_check_hourly', 'fep_event_check' );

/**
 * REMOVE CRON JOB ON PLUGIN DEACTIVATION
 */
register_deactivation_hook( __FILE__, 'fep_remove_hourly_backup_schedule' );

function fep_remove_hourly_backup_schedule(){
  wp_clear_scheduled_hook( 'fep_event_check_hourly' );
}

/**
 * EVENT CHECK FUNCTION
 */
function fep_event_check() {
    // GET ALL EVENTS POSTS
    $args = array(
        'post_type' => 'event'
    );
    $event_posts = get_posts($args);
    // LOOP THROUGH ALL EVENTS POSTS
    foreach($event_posts as $post) {
        // SETUP THE POST DATA
        setup_postdata( $post );
        $event_type = get_field( 'event_type', $post->ID );
        $recur_day = get_field( 'recurring_day', $post->ID );
        $start_date = get_field( 'start_date', $post->ID );
        $updated_recur_date = date('Ymd', strtotime('next ' . $recur_day));
        $today = date('Ymd');
        // CHECK IF POST IS A RECURRING POST IF START DATE IS NOT EQUAL TO TODAY'S DATE AND
        // START DATE IS LESS THAN (IN THE PAST) THAN THE NEXT DATE THIS EVENT IS SET TO RECUR
        if ( $event_type == 'recur' && $start_date != $today && $start_date < $updated_recur_date ) {
            // UPDATE THE START DATE TO THE NEXT DATE THIS EVENT IS SET TO RECUR
            update_field('field_60f8548e23cef', $updated_recur_date, $post->ID );
        }
    }
    // RESET THE POST DATA
    wp_reset_postdata();
    // NEED TO INCLUDE WP_DIE FOR AJAX CALLBACK FUNCTION
    wp_die();
}

0
Jackie 2 weeks 2021-11-12T11:46:17-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse