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!