Support

Account

Home Forums ACF PRO Change another field based on Date

Solving

Change another field based on Date

  • Hi

    I have a job listings post type and have 2x custom fields:
    Closing date – Date Picker field, 'job_listing_closing_date'
    Job Status – Radio (Open & Closed) 'job_listing_job_status'

    The user can manually choose Open or Closed of course, but is there a way that if the date from the Date Picker field passes, the Radio option changes to closed from the default open?

    Thanks

  • Hi @huwrowlands

    I think you need to use the cron job and check if the closing date for each post has passed or not. If the date has passed, you can update the other fields by using the update_field() function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/update_field/.

    To learn how to use the cron job, please take a look at this page: https://www.smashingmagazine.com/2013/10/schedule-events-using-wordpress-cron/.

    I hope this helps 🙂

  • I managed to get it to work using this bit of code. Not sure if a cron job would be a much better solution though?

    		<?php 
    			/*
    				Check if the job listing has closed (manually or automatically based on date). If so, add text to end of the title and change the label status.
    			*/
    			
    			// Setup checks for todays date and datepicker date in backend.					
    			$today = date( 'Ymd' );
    			$expire = get_field( 'job_listing_closing_date', false, false );
    			
    			// Change Job Status field in backend to Closed if date has passed.
    			$status = get_field( 'job_listing_job_status' );
    			if ( $expire < $today ) :
    				$status = 'Closed';
    				update_field( 'job_listing_job_status', $status );
    			endif;
    		?>
  • Hi @huwrowlands

    Could you please tell me where do you put that code? If you put it in single.php file, then that code will only get executed when someone is visiting the post. If you need the current status on other pages, I believe it will show the old status as it’s not updated yet. But if you only show the status on the post page, then I think it’s OK.

    I hope this makes sense 🙂

  • Hi @james

    I have placed it into single-job-listings.php file. There is a possibility that I’ll be using a custom template or archive page to display a list of these job listings too.

    The trouble seems to be that the backend doesn’t update unless someone views the front-end. So maybe, the only solution is a cron job?

    Thanks

  • Hi @huwrowlands

    In that case, I’m afraid yes, you need to use a cron job instead.

    Thanks 🙂

  • @huwrowlands I try your code but it’s not working for me. Can you make it easier than when the date has gone it shows closed text, instead of creating a new field for OPEN & CLOSE? But I try with Radio fields also and it’s not working.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Change another field based on Date’ is closed to new replies.