Support

Account

Home Forums ACF PRO Must update post twice to get values passed

Solving

Must update post twice to get values passed

  • I have a date-picker field that updates the custom post types title and slug. The only problem is, I have to update the post TWICE to get it to work.

    Someone mentioned that the value isn’t being saved the first time around and therefore the need for the updating twice. How do I modify my code below to make this work? Any help would be greatly appreciated!

    function my_acf_update_value( $value, $post_id, $field ) {
    	
    	$date = strtotime(get_field('date', $post_id));
    	$formatted_date = date('d F Y', $date);
                    $new_title = $formatted_date;
    
    	// update post
    	
    	$my_post = array(
    	'ID'           => $post_id,
    	'post_title' => $new_title,
    	'post_type' => 'usa',
    	'post_name' => $new_slug
    	);
    	
    	// Update the post into the database
    	wp_update_post( $my_post );
    	return $value;
    	}
    	
    	add_filter('acf/update_value/name=date', 'my_acf_update_value', 10, 3);
  • the update_value filter runs before the value is saved so in your function you are attempting to get a value that does not exist yet. The value is passed to your function in $value but you aren’t using it, is there a reason for this?

    Try the following. You may need to do some additional stuff, not sure. The value stored by acf for dates is YYYYMMDD

    
    unction my_acf_update_value( $value, $post_id, $field ) {
    	
    	$date = strtotime($value);
    	$formatted_date = date('d F Y', $date);
    
  • John,
    Thanks for your response.

    Basically, I have a date-picker custom field that should update the post title and post slug on update. It was working fine but the formatting was off–always displaying the date as YYYYMMDD.

    I later got the formatting to work, but it created another problem—the date isn’t being saved the first time around and therefore the need to update twice.

    This issue is beyond me. I hope that clarifies.

  • That’s what’s in my reply above.

    The problem is that the update_value fires before that value is saved to the database.

    This means that when you do get_field to get the value you are getting the value it was from the previous update. If it’s a new post then you get nothing, if you change the date you will get the previous date.

    The hook passes the new value as $value so you should be using that for the value to update the title

    
    function my_acf_update_value( $value, $post_id, $field ) {
    	
            // ** change this line
    	$date = strtotime($value);
    
    	$formatted_date = date('d F Y', $date);
                    $new_title = $formatted_date;
    
    	// update post
    	
    	$my_post = array(
    	'ID'           => $post_id,
    	'post_title' => $new_title,
    	'post_type' => 'usa',
    	'post_name' => $new_slug
    	);
    	
    	// Update the post into the database
    	wp_update_post( $my_post );
    	return $value;
    	}
    
  • John,
    I tried your code a few times and it produced the same results. I still had to update twice to get it working.

    I’m not knowledgeable on how to update the code to make it work. Should the $value to be equal to something?

    Thanks

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Must update post twice to get values passed’ is closed to new replies.