Created
July 29, 2012 14:23
-
-
Save zanematthew/3199216 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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