Support

Account

Home Forums General Issues How can I update one acf value with another on save?

Solved

How can I update one acf value with another on save?

  • Hi,

    Don’t know if this is even possible but here’s an explanation.

    The theme I’m using is a block theme (GeneratePress Pro).
    I don’t use any pagebuilder of any kind other than what’s shipped with GeneratePress (GP).
    I’m using a custom post type named ‘realestate’.

    With ACF I have created:

    • 1 Google map field (field name ‘location’)
    • 1 text field (field name ‘street_name’)

    By using the Google map field I can get the field values I need when working directly in my template files. However now I need to grab the values while in the block editor.

    GP offers to insert “dynamic data” from a custom field (of type ‘string’, not an array). This is why I have also created a simple text field.
    My idea is to somehow extract the value from Google map field – $location[‘street_name’] and update the text field value by using a method like update_value or similar. The problem is, I don’t know how to use the method or even if it’s the right one to use.

    How can I make sure that the text field gets updated with the value from $location[‘street_name’] whenever I save the current post?

    Thank you!

  • I have tried with the following instead:

    
    // Testing to save/update field value using another field value
    function my_acf_save_post( $post_id ) {
    
        
    // Get newly saved values.
        $values = get_fields( $post_id );
        $location = get_field('location', $post_id);
        $street_name = get_field('street_name', $post_id);
    
    	if ( ! get_post_type == 'realestate') {
    	   // Stop right away
    	   return;
    	}  else {
                    // Check the new value of a specific field.
    		if ($location['street_name']) {
    		   $street_name = $location['street_name'];
    		}
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post');
    

    Of course this is not doing anything because I don’t understand the documentation properly. Please help.

  • An ACF GM field stores an array with 3 indexes: “lat”, “lng”, “address”.

    to set a value of a field based on another field first you get the value and then you use update_field() to update the other.

  • I don’t get the documentation for acf/update_field() either. It says the $field parameter is the field array containing all settings. All the settings of what? The field I want to collect data from? In that case it seems this should work but it doesn’t.

    
    function save_updated_acf_fields($location)
    {
        // Collect GM field data
        $location = get_field('field_634147f894e08', $post_id);
        $gmap_street_name = $location['street_name'];
        $gmap_street_nbr = $location['street_number'];
    
        // Fields retrieving updated values
        $empty_street_name = get_field('field_6341483d94e09', $post_id);
        $empty_street_nbr = get_field('field_634151528a090', $post_id);
    
        if ($location) {
            $empty_street_name = $gmap_street_name;
            $empty_street_nbr = $gmap_street_nbr;
        }
    }
    
    add_filter('acf/update_field ', 'save_updated_acf_fields', 10, 1);
    

    Would you please translate the documentation into something a plain dummy like myself can understand?

  • You had the correct filter the first time “acf/save_post” because you will be working with multiple values. The other field may or may not have been saved when the acf/update_field hook happens for this field.

    You cannot update a field value simply by setting a variable equal to something, you must call update_field() to have ACF save that value to the DB.

  • Simple example:

    
    add_action('acf/save_post', 'my_acf_save_post', 30);
    function my_acf_save_post($post_id) {
      // get value from a field
      $value = get_field('field_name', $post_id);
      // update another field to this value
      update_field('other_field', $value, $post_id);
    }
    
  • Thank you John! It really wasn’t that clear to me that I had to use update_field() inside acf/save_post. It isn’t all too obvious in the documentation, at least not to me.

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

You must be logged in to reply to this topic.