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' );
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 use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.