Ok, I have finally figured it out – this is what tripped me up:
– Field key isn’t generated until you add something via the GUI, once you do this you can find it in the database, I think the docs mention this.
– If its a subfield of a repeater field you are adding too, you need 2 keys, the first is for the repeater field, the second is the subfield. So the code to do this looks like:
update_sub_field(array('field_55fcc711c095d', 1, 'field_55fcc726c095e'),20956, 20967);
Where 20956 is an attachment id of an image, and 20967 is the post id for testing purposes.
This however Will not work Unless I add one image manually through the GUI.
———— UPDATE ————–
I worked out a workaround for the page by querying directly:
select * from wp_postmeta where post_id=$post_id AND meta_key LIKE 'images_%image%'
This query gets all the fields I need out, and I can retrieve the attachment IDs from it. This works fine for the page – however on the exact same post in the edit screen I cant see the images in the repeater field.
TL;DR – They are in the DB, I can get them on the page, but they don’t appear on the edit screen and aren’t retrievable using the_repeater_field.
Thanks for the reply, I read the documentation. The problem is that if I insert a post the field isn’t created so I cant get the field key. The issue isn’t updating the repeater fields, or having a field key. The issue is adding to the field – I hope that makes sense.
I am trying to work out a workaround, I am currently inserting dummy rows to at least make the entry appear in
wp_postmeta using the following code:
update_field('images','', $post_id); update_sub_field(array('images', 1, 'image'), '', $post_id);
images is the repeater field, and
image is the subfield
If I check the database however, the field key doesn’t exist its just an empty string.
SELECT meta_id, meta_key, meta_value FROM wp_postmeta where post_id=20963 AND meta_key like '%image%'
You would expect this to return an entry with a field key however it returns this:
mysql> SELECT meta_id, meta_key, meta_value FROM wp_postmeta where post_id=20963 AND meta_key like '%image%'; +---------+-----------+------------+ | meta_id | meta_key | meta_value | +---------+-----------+------------+ | 140515 | images | | | 140516 | _images | | | 140517 | _0_image | | | 140518 | __0_image | | +---------+-----------+------------+ 4 rows in set (0.00 sec)
The next step for me is to just do the query manually once I work out how ACF handles storing images in meta_value. This seems like a big limitation for anyone importing data and programatically inserting it into repeater fields, ive spend 2 days trying to find a work around 🙁 – I will work one out and post it here.
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!
© 2022 Advanced Custom Fields.