Hi there,
I’m having difficulty adding new rows to a repeater field. My code basically checks to see how many values an array holds and that starts a For loop using that information.
$arrayCount = count($feed['Images']);
for($i = 0; $i < $arrayCount; $i++) {
$key = 'field_583ea1bfe02e9';
$value = array(
array(
'order' => $feed['Images'][$i]['Order'],
'primary_image' => $feed['Images'][$i]['IsPrimaryImage'],
'id' => $feed['Images'][$i]['Id'],
'url' => $feed['Images'][$i]['Url'],
'document_type' => $feed['Images'][$i]['DocumentType']['DisplayName'],
'document_sub_type' => $feed['Images'][$i]['DocumentSubType']['DisplayName'],
)
);
update_field($key, $value, $propertyID);
}
It only adds the last value within the array and just bypasses the first values. So it seems that it’s just overwriting the previous values.
I have also tried using the add_row() function but this just seems to add the correct amount of rows found in the array but it doesn’t add the values so they are just left blank.
Any help at all would be much appreciated.
Hi @jamesgeorgedunn
It seems your code is creating a new value and replacing the repeater field for every image you have. You should do it like this instead:
$arrayCount = count($feed['Images']);
// set the variables first
$field_name = 'repeater_field_name';
$field_key = 'field_583ea1bfe02e9';
// get the existing value
$value = get_field($field_name, $propertyID);
// add the new values
for($i = 0; $i < $arrayCount; $i++) {
$value []= array(
'order' => $feed['Images'][$i]['Order'],
'primary_image' => $feed['Images'][$i]['IsPrimaryImage'],
'id' => $feed['Images'][$i]['Id'],
'url' => $feed['Images'][$i]['Url'],
'document_type' => $feed['Images'][$i]['DocumentType']['DisplayName'],
'document_sub_type' => $feed['Images'][$i]['DocumentSubType']['DisplayName'],
);
}
// update it
update_field($key, $value, $propertyID);
Don’t forget to change the “repeater_field_name” value with your repeater field name.
I hope this helps 🙂
Lovely, thank you James.
I noticed that you also took it out of an array to.
I did manage to get this working using the update_row() function too which I think may be a better way of going about it.