Have you had a ton of posts that contain data on your custom fields? For any reason, you may want to rename one of the fields without having an effect on existing data. In this post, we'll show you two ways to update your field ID without losing data. Let’s see how!

Reminder: Before going to update the field ID, make sure you backup your database first. The methods described here involve running SQL on your database, which can’t revert. Having backups might help you in the event that there's something wrong.

Running SQL in phpMyAdmin

Assuming you want to replace the field’s ID location with the new address.

In phpMyAdmin, you can directly run the SQL UPDATE statement to perform the change. Meta Box saves each custom field in one row in the post meta table, where the field id is the meta_key and the value will be the meta_value in the meta table.

So, in phpMyAdmin you only need to run the following SQL:

UPDATE wp_postmeta SET meta_key = 'location' WHERE meta_key = 'address'

Just go to tab SQL and enter the SQL query above, then click the Run button to execute it.

Run SQL update in phpMyAdmin

Using a Functionality Plugin

If you can’t access the phpMyAdmin, you can create a functionality plugin that can help you perform the same SQL query.

Create a PHP file with the following content:

<?php
/**
* Plugin Name: Update Your ID
* Description: This is the plugin to help you update your ID.
* Version: 1.0.0
* Author: eLightUp
**/

function update_your_id() {
    global $wpdb;
    $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = 'location' WHERE meta_key = 'address'" );
}

update_your_id();

We name the plugin Update Your ID. It's very simple. It has only the update_your_id function which runs the SQL query to perform the database update. The SQL query is exactly the same that we have in the previous method.

After creating the file, upload it to the wp-content/plugins folder. Then go to Dashboard > Plugins and activate it. Once activated, the plugin will execute the SQL and change your field ID from location to address.

Then, if you don't want it to run every time your site loads, don't forget to deactivate the plugin. Running it only once is enough.

That’s all, is it simple? If you have any questions, feel free to let us know in the comment section. Good luck!

1 thought on “How to Change the ID of a Meta Box Field

  1. The SQL query seems to be backwards based on the description of "Assuming you want to replace the field’s ID location with the new address." So I believe you mean to say instead "Assuming you want to replace the field’s ID address with the new location." as then the SQL queries would make sense.

Leave a Reply to Dustin Dauncey Cancel reply

Your email address will not be published. Required fields are marked *