admin管理员组

文章数量:1134248

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.

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.

Share Improve this question edited Aug 27, 2023 at 1:56 grzybowski1911 asked Aug 27, 2023 at 1:51 grzybowski1911grzybowski1911 898 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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++;

本文标签: advanced custom fieldsUploading images via ACF updatefield function