Support

Account

Home Forums Backend Issues (wp-admin) Update ACF Field on daily basis

Solving

Update ACF Field on daily basis

  • Hi guys,

    I have an ACF field which shows the difference in days between today and the post publish date. The calculation seems to work, because when I add a fixed date, the field changes.

    My problem now is that the field does not seem to update automatically each day. Does anyone know how I can achieve this?

    The code:

    
    	$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 );
    
  • I suspect you would need to run a CRON daily which would trigger a script/function.

    That script/function can then run the code to update your date.

    Where does $post_id come from? Is it a custom post or a WooCommerce order?

    Is it a published post or in a set status?

    Basically, your script could simply be a WP Query that loops whatever the post type is, it should then update the post meta as you add your code above in the loop

  • The post_id comes from a custom post type and the posts are all published.

  • 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!

  • Actually, this won’t work. Reason being, you need access to the order to get the date for comparison!

    So you have options:
    1) When you run the date difference checker the first time (as per your other post), I think I’d be inclined to have another custom field which stores the value from:
    $date_created = $order->get_date_created();

    OR
    2) When you run the date difference code for the first time (as per your other post), you store if post ID of the order. That way, you can query the order post ID to get the order completed date.

    In my mind, option 1 is far easier and easier to implement:
    1) Just an another update_meta line to your other code
    2) Add a get_field on the code I posted above to get the order completed date

    As I say, my only concern is how many posts you may need to return each day, may timeout or become slow

  • Oke, so to be sure.

    I added an extra line of code, which saves the publication date in an ACF field.

    $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 );
    	update_post_meta( $post_id, 'datum', $datetime2 );
    

    So the next step is to create a file, which I save in my WordPress directory. The file should be like you showed:

    
    <?php
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'groeiprocessen',
    	'fields' 		=> 'ids', //not sure what to add here
    	'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();
    
    	$datetime2 = get_field( "datum" );
    
    	$difference = $datetime1->diff($datetime2);
    
        $verschil = "difference " . $difference->days . " days ";
    
    	update_post_meta( $post_id, 'datum2', $verschil );
    
    	endwhile;
    endif;
    

    The last step should be to trigger the URL with the Cron Job.

    Is that correct? Thanks for your help!

  • Yep, pretty much it!

    So if you call it cron.php and add that to your theme file, then go to:

    https://www.domain.com/wp-content/themes/theme-name/cron.php

    It should run

    Just backup your DB before running it, just to be sure.

    Then check your posts to ensure it has the right dates.

    As I say, depending on post quantity, this may lead to time out issues in the future! I’d therefore look into batch processing which can run without as many issues

  • I’ll get the following error.

    Call to undefined function get_query_var()

    Should I replace the ‘paged’ value with something else?

  • You can try:

    <?php
    global $wp_query;
    #$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'groeiprocessen',
    	'fields' 		=> 'ids', //not sure what to add here
    	'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();
    
    	$datetime2 = get_field( "datum" );
    
    	$difference = $datetime1->diff($datetime2);
    
        $verschil = "difference " . $difference->days . " days ";
    
    	update_post_meta( $post_id, 'datum2', $verschil );
    
    	endwhile;
    endif;
  • Now I get the following error:

    Uncaught Error: Class ‘WP_Query’ not found

  • Ah! Add this to the top of the file (after the <?php):

    # Our include			
    require_once('../../../wp-load.php');

    I think you can uncomment the other bits:

    <?php
    # Our include			
    require_once('../../../wp-load.php');
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'groeiprocessen',
    	'fields' 		=> 'ids', //not sure what to add here
    	'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();
    
    	$datetime2 = get_field( "datum" );
    
    	$difference = $datetime1->diff($datetime2);
    
        $verschil = "difference " . $difference->days . " days ";
    
    	update_post_meta( $post_id, 'datum2', $verschil );
    
    	endwhile;
    endif;

    This assumes you’ve added the file (cron.php) into your theme dir. If you’ve added it within a directory in your theme (like includes), you need to amend the path to wp-load.php

  • Hm, for some reason it doesn’t see the first parameter as a datetime.

    DateTime::diff() expects parameter 1 to be DateTimeInterface, null given in on line 23.

  • Check this post

    Because you are passing string whereas date_diff expects datetime object,

    
    $date_expire = '2014-08-06 00:00:00';    
    $date = new DateTime($date_expire);
    $now = new DateTime();
    
    echo $date->diff($now)->format("%d days, %h hours and %i minuts");

    I’d suggest you check the values you’ve got stored. My hunch is $datetime2 isn’t the right format

  • Also add some debugging in, check your script outputs what it should:

    <?php
    # Our include			
    require_once('../../../wp-load.php');
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'groeiprocessen',
    	'fields' 		=> 'ids', //not sure what to add here
    	'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();
    	echo '<p>Post ID: '.$post_id.'</p>';
    
    	$datetime1 = new DateTime();
    	echo '<p>Date time 1: '.$datetime1.'</p>';
    
    	$datetime2 = get_field( "datum" );
    	echo '<p>Date time 2: '.$datetime2.'</p>';
    
    	$difference = $datetime1->diff($datetime2);
    	echo '<p>Difference: '.$difference.'</p>';
    
        $verschil = "difference " . $difference->days . " days ";
    	echo '<p>Difference: '.$verschil.'</p>';
    
    	update_post_meta( $post_id, 'datum2', $verschil );
    
    	endwhile;
    endif;
Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.