0

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();
}
2
  • Nothing in error logs? Does it work without the wp_die? i don't see why that would make a difference but I'd try it anyway. Incidentally, your get_posts will default to only 5 posts without numberposts.
    – vancoder
    Nov 12, 2021 at 18:34
  • Thanks for the heads up on numberposts! I tried to remove wp_die and still wasn't getting anywhere. I don't see anything in errors. I just ended up changing the cron-job.org ping to hit /wp-admin/admin-ajax.php?action=fep_event_check and that ended up working perfectly. I still don't understand why this one wasn't though...
    – Jackie
    Nov 12, 2021 at 19:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.