Support

Account

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!