Convert one post type to another another

If you can’t tell yet, we’re a huge fan of custom post types in our theme development and blog tutorials.  They allow us to create feature rich admin areas while keeping with the intuitiveness that is WordPress; so, of course, we dig em!  Have you ever created a custom post type, but wanted to convert a bunch of already written posts into that new post type?  To accomplish this task, we’ve used a Post Type Switcher in the past, however, as of WordPress 3.1 that plugin no longer works so we needed to get down and dirty in the database for this one.  Here is how you do it:

  1. Backup your database just in case you need to roll back for some reason, we’ve never had a problem running the SQL below, but its generally a good idea to back up the DB when performing any action like this.
  2. Run the SQL below in phpMyAdmin, tweaking it to your needs first.
UPDATE wp_posts p
JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
JOIN wp_terms t ON (tt.term_id = t.term_id)
SET post_type = 'event'
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND tt.taxonomy = 'category'
AND t.name = 'Events'

The code above takes posts in the “post” post type that are “publish”ed and in the category “Events” and converts them to the post type “event”. Yes, we’re using The Events Calendar as an example yet again.  Now your events that used to be grouped in with all your other posts now have a neat new home under their own Event post type menu in the admin, making tutorials for clients that much easier!

Check out this comments...

  1. Awesome, this just saved my day, thanks!

  2. Valentin on August 2nd, 2013

You diggin' our post? Leave a comment and let us know...