Support

Account

Home Forums ACF PRO update_field with nested groups and repater fields.

Solving

update_field with nested groups and repater fields.

  • Hello,
    Is there any way to update a field that is nested in:
    group > repeater > group > field
    so
    company > company_addresses > address > formatted_address

    $company = 'field_5b0bcd26a5931';
    $vals[] = [
    	'company_addresses' => [
    		'address' => [
    			'formatted_address' => $geocode['formatted_address'],
    			'country' => $geocode['country'],
    			'country_code' => $geocode['country_code'],
    		]
    	]
    ];
    update_field( $company, $vals, $post_id );

    how can i achive that ?
    Thanks,
    Pepe

  • I’m trying another approach but without luck…

    if( have_rows( 'company', $post_id ) ):
    	while( have_rows('company', $post_id ) ) : the_row(); //group field
    		if( have_rows('addresses') ):
    			while( have_rows('addresses') ) : the_row(); //repeater field
    				if( have_rows('address') ):
    					while( have_rows('address') ) : the_row(); //group field
    						update_sub_field( 'formatted_address',  $geocode['formatted_address'] );
    					endwhile;
    				endif;
    			endwhile;
    		endif;
    	endwhile;
    endif;

    Any idea ?
    Thanks

  • With the second approach i get true
    var_dump(update_sub_field( 'formatted_address', $geocode['formatted_address'] ));
    But nothing is saved

  • well I tried all the possibilities without luck…
    So to fix this I used the update_post_meta function.

    $addresses = get_field( 'company', $post_id  );
    $counter = 0;
    foreach ( $addresses as $key => $address) {
    	foreach ($address as $k => $adr) {
    		$field = 'company_addresses_' . $counter . '_address_';
    		$counter++;
    		if( $adr['address']['geocode'] ){
    			$gaddress = $adr['address']['geocode']['address'];
    			if( $geocode = $this->getGeoData( $gaddress ) ){
    				$fields = [
    					'formatted_address' 	=> $geocode['formatted_address'],
    					'country' 				=> $geocode['country'],
    					'country_code' 			=> $geocode['country_code'],
    				];
    				$company = get_field('company');
    				foreach ($fields as $key => $value) {
    					update_post_meta( $post_id , $field . $key, $value );
    				}
    
    			}
    		}
    	}
    }
  • Sorry for the late reply to this, but using your example

    updating any field that is in the form of a repeater, and this includes a group field, the format of the value needs to be

    
    $value = array(
      // this is an array holding each row of the repeater
      array(
        // this is an nested array that holds the values for 1 row
        // each row need a field name/key => value pair for each field of the row
        'field key/name 1' => 'field 1 value',
        'field key/name 2' => 'field 2 value'
      )
    );
    

    that is if you want to update the entire repeater in one go. You basically have repeaters nested 3 deep. But there is a change to the group field in that it only has one row. So updating a group field would look something like

    
    $value = array(
      'field key/name 1' => 'field 1 value',
      'field key/name 2' => 'field 2 value'
    );
    

    putting this together for

    
    group > repeater > group > field
    company > company_addresses > address > formatted_address
    
    
    $value = array(
      // this is the array for the top level field "company"
      
      // next we have the field in the repeater
      'company_address' => array(
        // this field is a repeater, so each row need a nested array
        array(
          // first row of repeater
          'address' => array(
            // address is a group field
            // so now we need to give more name/value pairs
            'formatted_address'=> 'value',
            'country' => 'value',
            'country_code' => 'value'
          )
        )
      )
    )
    

    And if your confused, it’s no wonder. To top this off
    1) if no values exist yet for any of the fields/levels then you need to use field keys for everything
    2) if the field already has existing values then you need to first get all of the existing values and put them into arrays and then add the values you want to add to them.

    there are other functions like update_row(), update_sub_row(), add_row(), and add_sub_row(), but I really don’t know how these would work with nesting this deep. I honestly don’t use them, because I don’t thing they are documented very well and they are not documented for nesting 3 or more deep, so you’ll need to do a lot of digging and testing to get it to work.

    As far as the way that you’re doing it though, if this is a field that does not exist yet then you’ll run into issues unless you also create the ACF field key reference, and you also need to insert the count of the rows for each level. I’d map this all out for you but it’s far too complex. I would suggest that you actually save a post in the admin with values for each of the fields and then look in the postmeta table at what ACF creates. If you’re not creating all of the needed data using update_post_meta() then you’ll end up having issue when you try to display the information.

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

The topic ‘update_field with nested groups and repater fields.’ is closed to new replies.