DEV Community

Cover image for How to change the ID of a Meta Box field
WP Meta Box Plugin
WP Meta Box Plugin

Posted on • Originally published at metabox.io

How to change the ID of a Meta Box field

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!

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 👀

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay