Support

Account

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

  • The problem with crons is you can’t get any feedback on what’s actually happening.

    You could try setting the type of field to number, but I’m not sure that will help anything. You could also try using update_post_meta() instead of update_field().

    I also altered the query to get all posts, maybe some of them are being updated but only the most recent 10 or whatever the limit is set by you’re theme for the number of posts to display.

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

    If that doesn’t work, this is a simple function I use so that I can get data and see what’s going on during processes like crons and ajax requests. It creates a file in the same folder as the file that you place it in and can be used to output things along the way to see if it’s working or what could be going wrong. It will create a new file every hour so that it doesn’t get out of hand.

    
    function write_to_file($value) {
      // this function for testing & debuggin only
      $file = dirname(__FILE__).'/process_data'.date('YmdH').'txt';
      $handle = fopen($file, 'a');
      ob_start();
      
      if (is_array($value) || is_object($value)) {
        print_r($value);
      } elseif (is_bool($value)) {
        var_dump($value);
      } else {
        echo $value;
      }
      echo "\r\n\r\n";
      fwrite($handle, ob_get_clean());
      fclose($handle);
    }
    

    Then you can do something like this in your cron function

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