Home Forums ACF PRO Cron Jobs & Date Checking Reply To: Cron Jobs & Date Checking

  • Hello,

    How did you make that work? I used your code for a similar function but i cannot get it to work.

    My case is that we have 2 Post Categories. 1. Upcoming Events and 2.Passed Events.

    We create a post with ACF event_end_date which is an ACF Date picker. What we need to do is have a cron job that checks if the event_end_date has passed so it updates the Post Category from Upcoming Events to Passed Events. I just cant make it work. Here is the code if you guys got any input i would be thankful!

    /*  Scheduled Action Hook */
    function check_event_end_date() {
      global $post;
      $args = array( 
        'post_type' => 'post', 
        'post_status' => 'publish',
         'category__in' => array(33), /*Upcoming Events Category ID*/
        'posts_per_page'  => -1,
      $listings = get_posts( $args );
        foreach($listings as $post) : setup_postdata($post);
      $today = date( 'Ymd' );
      $expire = get_field( 'event_end_date', false, false );
            if ( $expire > $today ) :
                $post_categories="36"; /*Passed Events Category ID*/
                wp_set_post_categories( $post_ID, $post_categories, $append );
    // Schedule Cron Job Event
    if ( ! wp_next_scheduled( 'event_end_date_cron_job' ) ) {
        wp_schedule_event( date( 'Ymd' ), 'daily', 'event_end_date_cron_job' );
    add_action( 'event_end_date_cron_job', 'check_event_end_date' );