Support

Account

Home Forums General Issues Populate field value with value of a different field?

Solved

Populate field value with value of a different field?

  • I need to dynamically populate the default value of a field named “contact_info” with the value of a different field called “center_address” on a different page. I found code for populating the default value dynamically, but I can’t seem to get it working. Can you tell me what I’m doing wrong with the below code?

    $address = get_field('center_address', 342);
    	
    add_filter('acf/load_field/name=contact_info',
         function($field) use ($address) {           
    	     $field['default_value'] = $address;
    	     return $field;
         }
    );

    I tried using the_field instead of get_field, but that didn’t work either.

    This code is in functions.php. I’m trying to target a backend form on the post editing screen. The fields are text fields. I am using ACF Pro.

    I appreciate any help!

  • Nevermind! I figured it out myself. I needed to format it like this:

    function my_acf_load_field( $field ) {
    		
         $address = get_field('center_address', 342);
    		
         $field['default_value'] = $address;
         return $field;
    		 
    }
    
    add_filter('acf/load_field/name=contact_person', 'my_acf_load_field');
  • However — it brings me to a new question. Is this super resource intensive? This contact_person field that I’m populating is present on a LOT of posts.

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

The topic ‘Populate field value with value of a different field?’ is closed to new replies.