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 badges1 Answer
Reset to default 0Should 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
版权声明:本文标题:advanced custom fields - Uploading images via ACF update_field function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736823106a1954371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论