Support

Account

Home Forums ACF PRO Cron Jobs & Date Checking

Solved

Cron Jobs & Date Checking

  • Hi

    I am trying to setup a cron job for a custom post type (Job Listings), that contains two ACF custom fields.

    1. Datepicker – User can choose closing date of the job ( ‘job_listing_closing_date’ )
    2. Radio field – Open & Closed choices. ( ‘job_listing_status’ )

    I need the radio field to change from ‘Open’ to ‘Closed’ in the backend post edit screen if the job_listing_closing_date has passed. Here is my code at the moment which is located within ‘/wp-content/themes/themename/assets/functions/cpt-job-listings.php file.

    I’ve added the below to code to the website but nothing happens.
    Maybe the query is wrong or the ACF fields aren’t available in the file I have coded in?

    On a slight sidenote: Is there a way of testing a ‘daily’ cron job, more quickly to know that it works?! Perhaps setting it to hourly? But then how would that work with a date based query?

    // Scheduled Action Hook
    function check_job_end_date( ) {
    	// Variables
    	$today = date( 'Ymd' );	
    	$expire = get_field( 'job_listing_closing_date', false, false );
    	$status = get_field( 'job_listing_job_status' );
    	
    	// Query
    	$listings - new WP_Query(
    		array(
    			'post_type' => 'job_listings',
    			'meta_query' => array(
    				'key'     => 'job_listing_closing_date',
    				'value'   => $today,
    				'compare' => '<'
    			)
    		)
    	);
    	
    	// Conditional
    	if ( $expire < $today ) :
    		$status = 'Closed';
    			update_field( 'job_listing_job_status', $status );
    	endif;	
    	
    }
    
    // Schedule Cron Job Event
    function job_listing_cron_job() {
    	if ( ! wp_next_scheduled( 'check_job_end_date' ) ) {
    		wp_schedule_event( date( 'Ymd' ), 'daily', 'check_job_end_date' );
    	}
    }
    add_action( 'wp', 'job_listing_cron_job' );
  • 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();
      }
    }
    
  • Thanks.

    I have updated my code and installed a Cron plugin that will run any cron set but the radio field doesn’t updated to ‘Closed’.

    Both the current date and stored custom field date are the same format:
    ‘Ymd’

    Any ideas?

  • 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();
      }
    }
    
  • Thanks for the help. Sadly the file hasn’t been generated so am still stuck!

  • My guess here is that the cron is not running. This could be do to an error in php or some other problem. You might try turning on error reporting and error logging to see if that turns up any problems. https://codex.wordpress.org/Debugging_in_WordPress

  • I ended up re-writing most of the code and this is what worked in the end:

    // Create a cron job in order to check the custom field of 'job_listing_closing_date' against today's date. If the date has passed, set the job status to 'closed' and display different content on front-end.
    
    // Scheduled Action Hook
    function check_job_end_date( ) {
    
      global $post;
    		
      $args = array( 
      	'post_type'       => 'job_listings',
      	'posts_per_page'  => -1,
      );
      
      $listings = get_posts( $args );
    	foreach($listings as $post) : setup_postdata($post);
    
      $today = date( 'Ymd' );
      $expire = get_field( 'job_listing_closing_date', false, false );
    	$status = get_field( 'job_listing_job_status' );
    		if ( $expire < $today ) :
    			$status = 'Closed';
    			update_field( 'job_listing_job_status', $status );
    		endif;  
    	endforeach;
      
    }
    
    // Schedule Cron Job Event
    
    if ( ! wp_next_scheduled( 'job_listing_cron_job' ) ) {
    	wp_schedule_event( date( 'Ymd' ), 'daily', 'job_listing_cron_job' );
    }
    add_action( 'job_listing_cron_job', 'check_job_end_date' );
  • 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*/
                $append=false;
                wp_set_post_categories( $post_ID, $post_categories, $append );
            endif;  
        endforeach;
    
    }
    
    // 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' );
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Cron Jobs & Date Checking’ is closed to new replies.