How to Update an ACF Field Name Without Losing the Data

Advanced Custom Fields (ACF)

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.

  • new_field_name
  • old_field_name
  • 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'

All Done

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!

Subscribe
Notify of
guest

16 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Hélène
Hélène
1 year ago

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: field_subfield).

UPDATE wp_postmeta
SET meta_key = REPLACE(meta_key, 'old_str', 'new_str')
WHERE meta_key LIKE '%old_str%' 
bissy
bissy
4 months ago
Reply to  Hélène

Hi, This is so nice to me!!
I have to change subfield name, and I did it!

// step 1: change repeater parent name
UPDATE wp_postmeta
SET meta_key = REPLACE(meta_key, 'repeater_old_str', 'repeater_new_str')
WHERE meta_key LIKE '%repeater_old_str%'

// step 2: change repeater subfield name
UPDATE wp_postmeta
SET meta_key = REPLACE(meta_key, 'subfield_old_str', 'subfield_new_str')
WHERE meta_key REGEXP 'repeater_new_str_(.)_subfield_old_str'

// step3: update name filed in ACF Admin panel.

Thank you so much!

Johannes
Johannes
3 years ago

Thank you, Nathan, for posting this. It really helped me a lot and the whole migration I had to do went just fine.

Brad
Brad
1 year ago

Thank you for this. Very helpful. How would you modify the UPDATE statements for user meta fields?

Matthew
Matthew
1 year ago

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.

Chris
Chris
11 months ago

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?

Kenny
Kenny
1 month ago

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
Kenny
1 month ago
Reply to  Nathan Kinkead

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
Kenny
1 month ago
Reply to  Nathan Kinkead

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!