I have some acf fields attached to the user profile. Upon save, I want the content to copy to a CPT (breeder). I manage most values, but not the repeater.
This is my code:
$repeater = ‘_breeder_owner’; // the field name of the repeater field
$repeaterObjects = $values[‘field_61e1ea4738085’]; //getting the values from the $_POST[‘acf’] array
$name = ‘field_61e1ea5c38086’; //fieldname of name. The human name is breeder_owner_name
$address = ‘field_61e1ea6738087′; //the human name is breeder_owner_address
// loop through the rows
//size=f repeaterobject returns 2 which is fine
for ($i=0; $i<sizeOf($repeaterObjects); $i++) {
// building the field names of the repeater fields for the database
$name_key = $repeater.’_’.$i.’_’. $name;
$address_key = $repeater.’_’.$i.’_’. $address;
// I tried with human name, but it didn’t work either
// example
// update_sub_field(‘breeder_owner_name’, $values[‘field_….’], $id);
update_sub_field($name_key, $values[‘field_61e1ea5c38086’], $id);
update_sub_field( $address_key, $values[‘field_61e1ea6738087’], $id);
}
Any suggestions?
(I hook into the acf/save_post after tips from someone here (very much thank yoU!)
I figured it out. Here is the solution if someone is interested:
It was not so hard when I understood the process. I use a regular foreach to loop through the objects, and put the results in an array. Then I assign the array to the repeaterfield and the correct post. No need for super clunky $name_keys and separate update_sub_fields.
$repeater = 'field_61e1ea4738085'; // the field name of the repeater field
$repeaterObjects = $values['field_61e1ea4738085']; //getting the values from the $_POST['acf'] array
// loop through the rows
//size=f repeaterobject returns 2 which is fine
foreach($repeaterObjects as $obj) {
// building the field names of the repeater fields for the database
$breeder_data [] =
[
'breeder_owner_name' => $obj['field_61e1ea5c38086'],
'breeder_owner_address' => $obj['field_61e1ea6738087'],
'breeder_owner_postal_address' => $obj['field_61e1ea7a38088'],
'breeder_owner_email' => $obj['field_61e1ea8e38089'],
'breeder_owner_phone' => $obj['field_61e1eaa13808a'],
];
}
update_field( $repeater, $breeder_data, $id);