Have you had a ton of posts that contain data on your custom fields? For any reason, you may want to change the ID of the custom field without having an effect on existing data. But, if you only change the ID of the field from the field group settings, the data in the field will be lost. So, how does the data remain intact when changing the field ID? Let’s find it out.

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.

Video Version

Before Getting Started

Actually, we need the Meta Box plugin only for this practice. Since you have had custom fields on your site already, you obviously have the Meta Box as well.

In addition, I will create the field from scratch for illustration so you can have a clear overview. So, I have the Meta Box Builder to create and manage fields visually. It is an extension of Meta Box and available in the Meta Box AIO. In addition, you might want to use some advanced features from other Meta Box extensions, so you also might have them on your site.

Also, you need permission to access the database to check and see the changes in the data table.

Let’s start now!

Create Custom Fields

If you have had them already, move on to the next step. Otherwise, if not yet, just go to Meta Box > Custom Fields and create a new field group.

Go to Meta Box > Custom Fields and create a new field group

I will add a basic custom field for illustration purposes.

A basic custom field with the ID is address

Don’t forget to move to the Settings tab to set the location for the fields.

Move to the Settings tab to set the location for the field

The current ID of the field is address. After inputting data to the field in some posts, also for illustration purposes, I will take action to change this ID.

Now, just go to the post editor and input data to the field.

The data was inputted to the field

Change the ID of the Custom Fields

To avoid losing data, you not only change the ID in the field group settings but also need to change the meta_key of the field in the database to match the new ID.

In this article, I’ll change the field’s ID from address to location.

Change the ID of the Custom Field from the Field Settings

Go back to the field group to change the ID of the field. Just rename the ID.

Rename the ID in the field settings

Then, go to the posts, you will see that the change leads to the missing of all the data that you input to the field. The field in all the posts is blank after the change.

The inputted data is missing

If you only change the ID in this way, you must re-enter the data to each post.

Change the meta-key of the Field in the Database

Normally, Meta Box saves data of each custom field in one row in the post meta table.

Meta Box saves custom fields in the post meta table of the database

The good news is that even when you may not see the data in the fields, they're still stored in the database. You can see the data that I input into the field in the meta_value column, as well as the ID from the initial when I created the field in the meta_key column.

The field ID is stored in the meta_key column, and the data is in the meta_value column

Although I changed the ID of the field, it doesn’t affect the meta_key on this table. So, to change the field ID without losing data, you should change the meta_key as well. We can do it easily using a SQL query in the php My Admin or in the theme file.

Using the phpMyAdmin

If you have permission to access the phpMyAdmin, go to the SQL section of the table where you stored the data, and run the following query:

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

Run the SQL query to change the meta key from address to location

Then, click on the Go button to execute it.

Click the Go button to execute the query

After that, back to the table. The meta_key now is the new ID of the field.

The meta_key now is the new ID of the field

Adding Code to the Theme File

If you can’t access phpMyAdmin or are just familiar with using PHP, you can use the theme file to perform the same SQL query. The SQL query is exactly the same as we have in the previous method.

Go to the functions.php file, and add some lines of code:

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

update_your_id();

Add some code to the theme file to perform the same SQL query

Notice that if you change the theme someday, this will affect your data.

If you can check the data table in the phpMyAdmin now, you also will see the meta_key changed.

After changing the ID of the field both in the field settings and database, go to any post where the field is, you will see the data go back.

The data go back

That’s how we change the ID of any custom fields created with Meta Box without affecting the existing data in those fields.

Last Words

Updating the ID of a Meta Box field without affecting existing data is very simple but we cannot deny its importance.

You can create all commands for the field through the field ID, especially getting data, setting conditional logic, and getting field settings.

Also related to the database, converting cloneable fields to store values in multiple rows can be useful for managing data of custom fields. Feel free to your questions and requests in the comment below and follow our upcoming tutorials. Thanks for reading!

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

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