Update ACF field name without losing data
Posted on 11.06.2021
Remember to ALWAYS BACK UP YOUR DATABASE before running any SQL commands.
Check the old key
SELECT * FROM `wp_postmeta` WHERE meta_key LIKE '%old_field_name%'
Take note of the field key “field_” and a random string. You can also check this in ACF Field Group admin page.
Update postmeta
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'
Update ACF field keys
Make sure to have the correct field key (field_…)!
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'
Now you can rename the field to ‘new_field_name’ in ACF admin.