Support

Account

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

  • 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' );