Home › Forums › General Issues › Auto-Update Fields based on Expired Date › Reply To: Auto-Update Fields based on Expired Date
this is only an example, it will probably need to be adjusted based on your post types/fields.
function update_active_based_on_date() {
// set the date
$date = date('YMD');
// query to get all posts with date < today
$args = array(
'post_type' => array(/* list of your post types here */),
'posts_per_page' => -1,
'meta_query = array(
array(
'key' => 'fl_date',
'value' => $date,
'compare' => '<'
)
) // end meta_query
); // end $args
$query = new WP_Query($args);
// if posts are returned, loop over them and set active flag to false
if ($query->have_posts()) {
global $post;
while ($query->have_posts()) {
$query->the_post();
update_field('active_field_name', 0, $post->ID);
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
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.