Are you using ACF (Advanced Custom Fields) and have the need to change the field name, but then you discover that it makes all the data disappear?
Since ACF uses the field name as the meta key to save and retrieve data, it makes it seem impossible to change. You may consider changing only the label and leaving the field name alone.
However, in some cases, changing the name is needed. If you’re in that boat, I’ve got good news. It is possible to migrate all your post meta data to the new meta key.
In this post, I’ll show you exactly how to run a couple SQL statements on your database to migrate all the post meta data from the old key to the new key.
Backup Your Database
Whenever you are running database scripts, it is very easy to make a mistake and you can really mess up your site. Make sure you take a backup. You can use the UpdraftPlus Backup plugin, or you can export your database right from phpMyAdmin.
Don’t blame me if you mess up and didn’t take a backup!
Run These SQL Statements to Migrate Your ACF Data
Log in to your hosting control panel to access phpMyAdmin, the web interface for managing your MySQL database which WordPress runs on.
Click on your database on the left site, and then click the SQL tab at the top.
Note: in the following instructions, I’m using “wp_” as my table prefix. If your WordPress installation uses a different database table prefix from the standard “wp_”, then use your prefix instead.
Get Your ACF Field’s Reference Key
ACF fields have a reference key that starts with “field_” followed by random letters and numbers. Run this SELECT statement to find that reference key.
SELECT * FROM `wp_postmeta` WHERE meta_key LIKE '%old_field_name%'
You should see something like this.
In my example, the “old_field_name” is “video_url”. You will also see 2 meta key/value records for each post_id. One will have the meta_key exactly like you typed it with the meta_value that was entered in that ACF field. The other record will have an underscore before the meta_key and its meta_value is the ACF reference key you’re looking for. Take note of that reference key.
Migrate Your ACF Post Meta Data
Now, run the following UPDATE SQL script which will actually change the meta key (field name) from the old one to the new one. Make sure to replace the following placeholder values with your own.
- my_post_type = set this to the post type that you’re using your ACF field group with. If you’re using it for blog posts, then your post type should be “post”
UPDATE `wp_postmeta` as m JOIN `wp_posts` as p ON m.post_id = p.ID SET m.meta_key = 'new_field_name' WHERE m.meta_key = 'old_field_name' AND p.post_type = 'my_post_type'
Migrate Your ACF Field Reference Key Post Meta Records
Now, run the following UPDATE statement that will update all of those records with meta keys that start with an underscore and have the meta key with our field’s reference key.
Here are the placeholders you will need to change out.
- _new_field_name = should be your new field name with an underscore in front
- field_5af0d933478b4 = should be the reference key that you took note of earlier with the SELECT statement
- my_post_type = should be the same post type you used in the previous UPDATE statement
UPDATE `wp_postmeta` as m JOIN `wp_posts` as p ON m.post_id = p.ID SET m.meta_key = '_new_field_name' WHERE m.meta_value = 'field_5af0d933478b4' AND p.post_type = 'my_post_type'
That should be it. You should now see your old data showing up in your ACF fields with the new field name.
If you have any questions or need help, let me know in the comments below. Thanks!
Blogger, expert WordPress developer, and developer of the awesome bbPress Voting plugin which is a must-have plugin for any bbPress forum.
Download bbPress Voting for free on the WordPress Plugin Directory.
Not perfect, but I found this to work like a charm for ACF field that are in groups (where the meta key looks like this:
Hi, This is so nice to me!!
I have to change subfield name, and I did it!
Thank you so much!
Thank you, Nathan, for posting this. It really helped me a lot and the whole migration I had to do went just fine.
Great! Glad I could help!
Thank you for this. Very helpful. How would you modify the UPDATE statements for user meta fields?
User meta is handled a bit differently. I haven’t look into that yet.
With the SQL query I write the code in and then click the ‘Go’ button. Is that it?
I use the post type ‘attachment’ as the ACF fields are set to media attachments.
Does it work for sub fields in a Repeater ACF field.
I think I’ve done this for repeater subfields before, but I can’t remember for sure, and don’t remember if there were any tricks to it.
A slightly complicated case. I have a load of meta data that I want to convert to ACF. The reference key is missing so how do change your second UPDATE into an INSERT?
Hi Chris, that question may be answered better on the official ACF forum.
What if you don’t remember the original ACF field name? I changed the name a few times (indecisiveness 😬) in phpmyadmin in the table can you share what an ACF field looks like that has content assigned to it? This way I can manually copy the content over to the new fields?? Or how would you alter the script to move the content to the new fields? I have 10 fields in the database that have a series of letters and colons in the meta value column I believe. Help lol 😂
Kenny, my post shows a screenshot of what the ACF field looks like in the database. If you don’t remember what they were originally named, just look at all the records in post_meta with a certain post_id. If you recognize the values populated in those post_meta records, then you have the old meta key.
Thanks for the response, however, you don’t show what a serialized array looks like in your screenshot. Because my fields in question use the ACF relationship field type. I’m confused on how I’d be able to find the old one and move the relationship array data into the new one if I don’t remember what the old one/renamed field was? If the field is showing serialized array of data is that a great indication of it being the original fields and where the data currently is saved? What would be the query to move the serialized array data out and… Read more »
Kenny, sorry if I’m not quite understanding the issue you’re having. Serialized data is just the way that WordPress saves arrays of data. As opposed to converting it to JSON or something like that, it serializes it. WordPress has built-in functions to serialize and unserialize data. But, I don’t see what that has to do with renaming an ACF field. If you’re still just looking to find which meta key belongs to which field, if it’s a relationship field you’re looking for, just look at which post IDs that field is related to from a certain post, then search for… Read more »
Perfect, I was able to solve my issue, just so happens that after reviewing my php code, I was missing part of a script causing content to not connect 😵💫 wp_reset_query – on my while loop! Caused content to not show each posts relevant info. Thank you for the responses!