Home › Forums › General Issues › Auto Update Field based on ACF Date Picker field
Hey everyone,
i really need some help with my code.
I have a custom post type named “coupon”. With ACF i added two fields. The first is a Date Picker field named “coupon_valid_till” with my expiration Date. The second one is a true/false field named “is_expired”. I want to change the value of the true/false field to false if the expiration date is reached.
First i saw this thread:
https://support.advancedcustomfields.com/forums/topic/auto-update-fields-based-on-expired-date/
But that didn´t work.
I then found this pretty similar thread:
From the Answers in the Thread, this code should work. But i don´t need to change the Post from public to draft. So i changed this:
if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
to that:
if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
update_field('is_expired', 0, $post->ID);
}
But it dint´t work either. I managed to setup a cronjob for the code, which gets executed – but nothing happens.
It would be great if someone could help me.
Kathi
Ok. Problem solved.
I changed this:
if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
To that:
$expiredate = get_field('coupon_valid_till', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field itself
if (($expiredate < $today) && ($expiredate != "")) {
$field_key = "field_634c04868ed81";
$value = "1";
update_field($field_key, $value, $post->ID);
}
} // end while have_posts
wp_reset_postdata();
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 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.