Home › Forums › ACF PRO › update_field with nested groups and repater fields. › Reply To: update_field with nested groups and repater fields.
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!