Support

Account

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

  • You need to have a loop in order to get and update fields.
    Your query should already be returning the posts that you want
    to update and you shouldn’t need to test them again.

    
    // Scheduled Action Hook
    function check_job_end_date( ) {
      // Variables
      $today = date('Ymd');  
      
      // Query
      $listings = new WP_Query(
        array(
          'post_type' => 'job_listings',
          'meta_query' => array(
            'key'     => 'job_listing_closing_date',
            'value'   => $today,
            'compare' => '<'
          )
        )
      );
      
      global $post;
      if ($listings->have_posts()) {
        while ($listings->have_post()) {
          $listings->the_post();
          update_field('job_listing_job_status', 'Closed');
        }
        wp_reset_postdata();
      }
    }