Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thadallender/5781865 to your computer and use it in GitHub Desktop.
Save thadallender/5781865 to your computer and use it in GitHub Desktop.
Bulk convert any number of custom post meta keys for all posts in a specific WordPress post type. Change the $posttype, $old_prefix, $new_prefix and the $keys array at the top of the plugin file. Important: This plugin fires on the init hook, so only run it once (deactivate this plugin immediately after all custom post meta keys have been updated.
<?php
/**
* Plugin Name: GPP Bulk Convert Post Meta Once
* Plugin URI: http://graphpaperpress.com
* Description: Converts post meta from one key format to another
* Author: Thad Allender
* Author URI: http://graphpaperpress.com
* Version: 1.0
* Text Domain: gpp
* Domain Path: languages
*/
function gpp_bulk_convert_post_meta_run_once(){
// config
// the post type you want to change post meta keys for
$posttype = 'themes';
// the old post meta key prefix
$old_prefix = 'gpptmb_';
// the new post meta key prefix
$new_prefix = 'gpp_';
$args = array(
'numberposts' => -1,
'post_type' => $posttype
);
$posts = get_posts( $args );
foreach ( $posts as $p ) :
$keys = NULL;
// all keys you want to change, excluding their common prefix
$keys = array(
'download' => 'download-id',
'description' => 'theme-description',
'ft1' => 'feature-title-1',
'ft2' => 'feature-title-2',
'ft3' => 'feature-title-3',
'ft4' => 'feature-title-4',
'fd1' => 'feature-description-1',
'fd2' => 'feature-description-2',
'fd3' => 'feature-description-3',
'fd4' => 'feature-description-4',
'fi1' => 'feature-image-1',
'fi2' => 'feature-image-2',
'fi3' => 'feature-image-3',
'fi4' => 'feature-image-4',
'quote' => 'theme-quote',
'quoteattr' => 'theme-quote-attribution',
'quoteurl' => 'theme-quote-url',
'additional' => 'theme-additional-info'
);
foreach ( $keys as $key ) :
$meta = get_post_meta( $p->ID, $old_prefix . $key, true );
if ( $meta ) :
update_post_meta( $p->ID, $new_prefix . $key, $meta );
delete_post_meta( $p->ID, $old_prefix . $key );
endif;
endforeach;
if ( isset( $p ) && is_array( $p ) ) :
// save the post
wp_update_post( $p->ID );
// unset the post
unset( $p->ID );
endif;
endforeach;
}
// fires on the init hook by default, so only run this once!
add_action( 'init', 'gpp_bulk_convert_post_meta_run_once' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment