How To Copy or Move Old Meta key to New meta key

On one of my WordPress projects, I had to move (or copy) the value from an old meta_key to a new meta_key, the reason for that is because I needed the old meta_key to be used to store a different set of information. Problem I ran into was I had 3,000+ records associated with the old meta_key, so to do this manually would be a very time consuming task and not to mention it requires an exceptional data entry skill :).

As always I searched online to see if there is an easy way to do this in WordPress, unfortunately I couldn’t find anything (or maybe I just didn’t look far enough?) at the time of writing this. Since WordPress doesn’t provide any solution in my case, I turned my focus on the database area and fortunately I found a solution that phpMyAdmin could do this easily just by running a simply SQL query below.

NOTE TO BACKUP YOUR DATABASE BEFORE ANY TRIAL! Don’t say I didn’t warn you!

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)( SELECT post_id, 'newvalue', meta_value FROM wp_postmeta WHERE meta_key='oldvalue')

**Obviously change the newvalue to your new meta_key, oldvalue to your old meta_key.

In my case running the query above in phpMyAdmin basically created additional 3,000+ rows in the wp_postmeta table with new meta_id, but duplicated the post_id (copied from oldvalue), new meta_key with duplicate values copied over from the old meta_key. Make sense?

Now this may or may not work for you, I ask that you handle the SQL query with care, do back up before testing, last thing you want is losing all your data. Anyhow, if you know of a way to easily resolve the problem I had above with WordPress, post it below.

Leave a Reply