0

I am trying to update image fields within a group in ACF via the update_field function

I am successfully adding the images to the media gallery but I am having trouble getting them added to each ACF field

The following line is adding the images to the media library and returns the ID of each image

$gallery_img_id = rudr_upload_file_by_url($gallery_img["original_path"]);

Each ACF field is titled gallery_image_1, gallery_image_2, gallery_image_3 etc.

Based on what I've read from ACF documentation I should be able to create an associative array of data for each image and add them via update_field, I'm creating a blank array $gallery_data and pushing each key/value for the acf field and the attachment ID to the associative array

$acf_gallery_slug = "gallery_image_" .$counter;
$add_image = [ $acf_gallery_slug => $gallery_img_id ];
array_push($gallery_data, $add_image);

then using update_field to update each acf field

update_field('gallery', $gallery_data, $property_id);

However the fields are not getting updated, trying to figure out what I am doing wrong?

The gallery parameter in update_field is a group field in which the gallery_img_(num) fields are located/nested, and the $property_id is the id of each post I am trying to work with.

1 Answer 1

0

Should anyone come across the same issue, I found that when creating the $gallery_data array I was creating an array of arrays instead of key value pairs, I ended up just running the update_field function inside a foreach with the add_image key value pair created.

$gallery_img_id = rudr_upload_file_by_url($gallery_img["original_path"]);
$acf_gallery_slug = "gallery_image_" . $counter;
$add_image = [ $acf_gallery_slug => $gallery_img_id ];
update_field('gallery', $add_image, $property_id);
$counter++;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.