Skip to content

Instantly share code, notes, and snippets.

@zanematthew
Created July 29, 2012 14:23
Show Gist options
  • Save zanematthew/3199216 to your computer and use it in GitHub Desktop.
Save zanematthew/3199216 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type Converter
<?php
/**
* Usage: post_type_converter( 'tracks', 'venues');
*/
function post_type_converter( $from=null, $to=null, $post_id=null ){
global $wpdb;
$query = "SELECT ID, post_type FROM $wpdb->posts WHERE post_type = '{$from}'";
$result = $wpdb->get_results( $wpdb->prepare( $query ), 'ARRAY_A' );
if ( empty( $result ) )
return print "No post type '{$from}'";
$tmp_post['post_type'] = null;
foreach( $result as $r ){
$tmp_post['ID'] = $r['ID'];
$tmp_post['post_type'] = $to;
$id = wp_update_post( $tmp_post);
if ( $id ) {
print "Succesfully updated post_id: {$r['ID']} from: {$from} to: {$to}<br />\n";
} else {
print "Failed to updat post_id: {$r['ID']} from: {$from} to: {$to}<br />\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment