nooshu - Matt Hobbs' Web Development Blog

Kneeling on the shoulders of giants

Converting a WordPress post to a custom post type

Update: There is now a plug-in that does this for you called Post Type Switcher so you don’t have to get your hands dirty with the SQL (thanks Bruno!). Another plug-in called Convert Post Types is also available that does the job for you, Thanks to Steve for that one.

With the release of WordPress 3.0, developers now have the ability to create custom post types by adding a few lines of code to a themes functions.php. The feature is a huge step forwards for WordPress as it moves closer to being a viable CMS solution (without all the hacks). There have been a number of projects I’ve worked on that have required a custom post type; rather than relying on the client selecting the correct category under ‘Add new post’, which tends to always leads to issues.

I’m in the process of converting nooshu.com to use custom post types and taxonomies, the main area in particular is my portfolio section. At the moment the portfolio posts are just standard posts which have a category of ‘portfolio’ attached to them. It works, but I don’t get that warm fuzzy feeling, and it feels like a bit of a hack. So time to start using a custom post type of ‘portfolio’ (yay!).

Unfortunately at the moment there doesn’t seem to be a quick way of converting a published standard post into a shiny new custom post type, at least not from the administration area. After a little investigation work using phpmyadmin and the nooshu WordPress database, I discovered it’s actually quite easy. Be warned though, it’s easy to break your blog by editing the database directly, so make sure you back everything up. I take no responsibility for what happens if you break something!

To convert a post into a custom post type; in phpmyadmin look for the wp_posts table. Inside it do a search, where the ‘post_type’ value equals ‘post’. This will give you a list of all your posts (otherwise you will also have all the revisions and attachments listed). From the list you should be able to pick out the posts you want converting to your new post type, edit one of these posts and change ‘post_type’ from ‘post’ to your new custom post. In my case this would be ‘portfolio’:

phpmyadmin admin panel for a WordPress post

Change 'post_type' from 'post' to your custom post type.

Click save and go back to the previous page. Done! You should now see the post appear under your new custom post type in the WordPress admin panel. If you have any issues just change ‘post_type’ back to ‘post’ and it will reappear under the standard posts. No information is lost when changing the post type.

It’s not an ideal solution, hopefully it will be possible to do it from the administration panel in the future; but it does save having to re-enter all the posts under the custom post type for the moment.

Leads on June 24 10 / 174 Permalink

Thanks for this, it works fine. You have saved me hours and hours work :)

Matt on June 24 10 / 174 Permalink

No problem Tom, here’s hoping they add some functionality to do this from admin panel in a future release.

Matt on June 30 10 / 180 Permalink

Oh excellent! Thanks Bruno, I’ll update the original post with the details.

Bruno on July 6 10 / 186 Permalink

The plugin allow only per post changes, what is good, but, don´t solve all problems.

In the end I had to use phpMyadmin, I saw myself running a query. =)

I think there is no way to “convert” old posts based on their categories (using a plugin), yet.

Matt on July 26 10 / 206 Permalink

Thanks for the heads up Steve, that looks perfect! I’ll update the post for others viewing.

Jeffrey Lin on April 6 11 / 95 Permalink

found this site because Post Type Switcher doesn’t seem to be working with WP 3.1 upgrade. Doesn’t seem like a hard fix for that plugin so I’m sure the plugin author will get to it soon. Thanks for this advice to go directly into MySQL. Always good to know how to get to the nuts and bolts.

Matt on April 7 11 / 96 Permalink

Excellent, thanks for the heads up Jeffrey.

Opabinia on November 7 11 / 310 Permalink

Thanks a lot Nooshu. I haven’t tried the code yet but I find it wonderful that you wrote a solution to my pending dilemma (swichting from categories to post type). I can now go forward with my update.

Cheers mate

Leave a Comment

Your email will not be published. Required fields are marked *