Home › Forums › Add-ons › Gallery Field › Programmatically Inserting Photos Into ACF Gallery Field, But…. › Reply To: Programmatically Inserting Photos Into ACF Gallery Field, But….
A gallery field in ACF stores an array of image IDs. I believe that the following line overwrites what’s stored with the new value each time.
update_field("field_5693402ab8561", $attach_id, $post[0]->ID );
You will need to get what’s already in the field, append the new image to the array and then update it. Something like this might work.
$array = get_field('field_5693402ab8561', $post[0]->ID, false);
if (!is_array($array)) {
$array = array();
}
$array[] = $attach_id;
update_field('field_5693402ab8561', $array, $post[0]->ID );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.