Hi!
I’m hoping to create a one-time-use PHP script to update ACF data belonging to blocks, in a number of posts on a site I’m working on. The block in question has a repeater field with three fields, and I want that to be moved into a group.
Repeater field name: ‘rep_items’
Repeater subfields: ‘heading’ (Text), ‘content’ (Wysiwyg), ‘image’ (Image)
New group name: ‘acc_content’
The overall ACF field group has been updated, using drag-and-drop to move the existing repeater into the group. I have a high number of populated blocks across pages and posts in the site, which is why I’m very keen to use a script for this; I also hope to leverage it in future for similar tasks, so this is a learning experience too.
The issue I’m having is that it seems quite difficult to rearrange the data in this way. If I use parse_blocks() on my post->post_content, find the blocks I’m looking to modify, and examine block[‘attrs’][‘data’], the repeater’s content seems to be deconstructed and sits at the root level, with key string names that are indicative of the structure I’d expect to see when working at the post-meta level. I think I understand now that this is serialised for storage against Gutenberg blocks, and this might not happen if I were looking at a post’s ACF fields.
I’ve examined this same data on a block populated using my updated structure with the repeater inside a group, and I can see that the repeater field names are prefixed with the group, but not in a way that I believe can be parsed safely by ACF without reference to other fields. The data looks like this:
acc_content_rep_items_0_heading: "Title 1"
_acc_content_rep_items_0_heading: "field_somefieldid"
acc_content_rep_items_0_image: "625"
_acc_content_rep_items_0_image: "field_somefieldid"
acc_content_rep_items_1_heading: "Title 2"
_acc_content_rep_items_1_heading: "field_somefieldid"
There are also fields “acc_content” and “_acc_content”, which obviously represent the group. I can almost see the connections here, but I’m not certain on them.
This is what the content looks like when it IS adhering to the new structure, of course. What I want to do is migrate the data that doesn’t reference the ‘acc_content’ group. It won’t be sufficient to just build a fallback into my block’s PHP to find the old structure and render it; I want content authors to be able to go into blocks populated in the old structure, and see the content, not to have it hidden in legacy fields in the DB that are supported as a patch.
I feel as though, in the migration script, I may need to query the ACF field group definition – as opposed to just playing blindly with the data – if for no other reason than to know what field_ID to insert for the group. Or maybe I need to just go to my ACF field group’s JSON, take that field_ID, and manually add it and all the other internal, underscore_prefixed fields and construct this the hard way.
Are there any tricks or ACF library utility methods that I can employ to make this process easier for myself? What are your thoughts on doing this, generally?
Thanks for any assistance you can provide.
You must be logged in to reply to this topic.
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.