Home › Forums › Backend Issues (wp-admin) › Update ACF Field on daily basis › Reply To: Update ACF Field on daily basis
If you had a file like:
<?php global $wp_query; $paged = get_query_var('paged') ? get_query_var('paged') : 1; $args = array( 'posts_per_page' => -1, 'post_type' => 'your_cpt', 'fields' => 'ids', 'post_status' => array('publish'), 'paged' => $paged ); $wp_query = new WP_Query($args); if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); $post_id = get_the_ID(); $datetime1 = new DateTime(); $date_created = $order->get_date_created(); $datetime2 = new DateTime($date_created); $difference = $datetime1->diff($datetime2); $verschil = "difference " . $difference->days . " days "; update_post_meta( $post_id, 'datum2', $verschil ); endwhile; endif;
You can then run a cron to ping that URL, it would in theory then run the script.
However, depending on the volume of posts, over time, may be less efficient.
One other option is to look at running batch processing to do the same, but this is a lot more involved.
The above code is of course untested!
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.