I have a job listings post type and have 2x custom fields:
Closing date – Date Picker field,
Job Status – Radio (Open & Closed)
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?
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; ?>
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 🙂
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?
In that case, I’m afraid yes, you need to use a cron job instead.
@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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.