Jay Hoffmann

Books, movies, and code

Changing Field Keys in Advanced Custom Fields

I recently ran into a problem when using the Advanced Custom Fields plugin which is a bit esoteric. In the previous iteration of the site I was working on, fields were registered programmatically, using acf_add_local_field_group and acf_add_local_field. In the new version, we decided to move these back into the GUI on the admin panel.

For most of the fields, all we had to do was replicate the field group and field names / labels and everything worked fine, even on existing posts. However, repeatable and flexible fields would not show up for posts that had already been created.

The problem was with the field keys. When I created the fields in the admin panel, they were assigned a different field key, and thus existing content didn’t work anymore.

I found a post that outlined a fix on the plugin’s forums and it actually worked pretty well. Basically, you can go into the edit screen in the admin, and inspect element or use firebug to open up the HTML source. Find the HTML element that the repeatable parent field or flexible field is wrapped in, and remove the “meta” class from this. Then you can find the hidden field that has a the field key in it, and change it to the field key you had registered via PHP. Once that’s done just save the field group. For some reason, that works just fine.

Of course, another solution is to do a search and replace in the database. Just make sure you hit everything in the wp_postmeta table.

The main point is that if you run into a problem with Advanced Custom Fields transitioning from registering via PHP to using the admin panel, the problem with existing posts has to do with the field keys.