Change The meta_key Names For Multiple post_meta Custom Fields in WordPress

This is how you can change the meta_key names of post_meta custom fields for WordPress posts. You can change the meta_keys for just one custom field, or for multiple custom fields.

To clarify, post_meta are those custom fields of information which have been attached to a post by using either add_post_meta() or update_post_meta(). They are also called custom fields. The post_meta key ($meta_key) is the name of the custom field.

Why Do This?

There are several reasons why you may want to change the name of the post_meta keys. For example, you may have acquired some legacy code that you are revamping.

What This Code Does

This will migrate the post_meta value from the old key name to a new key name.

Then, the old post meta will be deleted. The post_meta will then only exist with its new name.

The value for the custom field will remain intact. The value will simply have a new name (key) by which to call it.

Where To Edit The Code For Your Use

  1. In this example, we assume that you have 4 post_meta keys that you want to change to some new key names. If you only need to change the name for one post_meta key, then delete lines 4–6.
  2. The first meta key that you want to change goes on line 3. On line 3, change _isa_old_metakey_1 to the name of your old meta key that you want to change. Change _isa_new_metakey_1 to the new name that you want to give it.
  3. If you have a second meta key that you want to change, then do the same thing on line 4, but do it for your second meta key that you want to change.
  4. On line 5, do the same for the your next meta key. On line 6, do the same for your next meta key. Add more of these lines for as many meta keys as you want to change. Again, if you only need to change the name for one post_meta key, then delete lines 4–6.
  5. This code applies to all pages and posts of all types, even custom post types. If you want this to only work for regular posts and pages, you can change line 13 to to:

    'post_type' => array('post', 'page'),

Nothing else needs to be edited in this snippet.

See more notes below.

$meta_keys = array(

	array( '_isa_old_metakey_1','_isa_new_metakey_1' ),
	array( '_isa_old_metakey_2','_isa_new_metakey_2' ),
	array( '_isa_old_metakey_3','_isa_new_metakey_3' ),
	array( '_isa_old_metakey_4','_isa_new_metakey_4' ),

);

foreach ( $meta_keys as $k ) {

	$args = array(
		'post_type'      => array_keys( get_post_types( array('public' => true), 'names' ) ),
		'posts_per_page' => -1,
		'post_status'    => 'publish',
		'meta_key' => $k[0]
   );

	$the_query = new WP_Query( $args );

	if ( $the_query->have_posts() ) {

		while ( $the_query->have_posts() ) {
		    $the_query->the_post();

		    $meta = get_post_meta( get_the_ID(), $k[0], true );
		  	
			if ( $meta ) {

				// Migrate the meta to the new name

				update_post_meta(get_the_ID(), $k[1], $meta );	// add the meta with the new name
				delete_post_meta( get_the_ID(), $k[0] );		// delete the old meta
				
			}
		}

	}

	wp_reset_postdata();	// Restore original Post Data

}

Note 1. This only needs to run once

You should not run that code on every page load. It only needs to run once. So, if you want to do this in a plugin or theme, you should make sure that it only runs once. There are several ways to implement that. Here is one way. Use this wrapper, and insert the code from above into line 8, here:

/**
 * Run this code only once
 */
function my_change_metakeys_once() {
	if ( get_option( 'my_change_metakeys_complete' ) != 'completed' ) {

		// PLACE YOUR CODE BELOW THIS LINE
		





		update_option( 'my_change_metakeys_complete', 'completed' );
	}
}
add_action( 'init', 'my_change_metakeys_once' );

Note 2. Remember to update the old meta keys names wherever they are used

Wherever you were using the value of the old custom fields, like this:

$custom_field = get_post_meta( $post_id, '_isa_old_metakey_1', true );

You must update it to use the new meta_key name, like this:

$custom_field = get_post_meta( $post_id, '_isa_new_metakey_1', true );

Now, I’m not talking about updating them on the code on this page. I mean that you must update them wherever else in your code you were using the old post_meta key names.

See more:

We've One Response

  1. February 4th, 2021 at 12:22 pm

    Hi Isabel,
    Thank you for the great article.
    Your suggested method works good, however, there’s a condition to take into account on line 28, which is when the value is actually equal to 0.

    ...
    if ( $meta ) {
                $meta = get_post_meta( get_the_ID(), $k[0], true );
                
                // CHANGE THIS
                if ( $meta ) { // This would return false for values equal to 0
                // INTO THIS
                if ( $meta || $meta === 0 ) { 
    
                    // Migrate the meta to the new name
    ...
    

    I hope this helps someone!

    Hassoun

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]