The Power of Custom Post Types in WordPress 3.0


There has been quite a bit of discussion — especially from a developer perspective — on the custom post types introduced in WordPress 2.9 and refined and made accessible in WordPress 3.0. They are a powerful addition to WordPress and opens up quite a bit of CMS-style possibilities in a powerful new way. However:

“Power corrupts. Knowledge is power. Study hard. Be evil.”

So, with that quote (found un-attributed on the interwebs), let us venture forth and learn about the power of custom post types.

What are custom post types?

In WordPress each time you write a post, page, draft, upload an image, or allow something to auto-save you create an entry in the “posts” table in your database. Each entry in this table has a type associated with it that allows WordPress to go back and search for posts limited by a particular type. The main loop in WordPress for example, searches the posts table for post_type=”post” and post_status=”publish”. Images have an entry under the post type of “attachment”. Pages get “page,” etc.

A custom post type is quite simply one that is not included with WordPress by default. Either you or a plugin have registered the post type to exist.

Why would I need a custom post type?

For many blogs and websites there is no need for creating various custom post types. Thinking about the type of information you want to put on your site, ask some questions:

  • Do I need discrete categorical or tag-type information applied to this information?
    (if yes, this is a good candidate for custom post types)
  • Is this a variation of a blog post?
    (if yes, use categories, tags, page templates, or other means to modify a post rather than anything custom)
  • Do I need to display traditional pages, blog posts, and a 3rd completely unrelated type of information?
    (if yes, you’re in the right place)

While creating a custom post type is much easier in WordPress 3.0 there’s still quite a bit of developer/designer work to be done. If your data type can fit within the existing schema of pages and posts: use those.

Do you have any examples?

I thought you’d never ask. I have three examples to show. The first where custom post types were used, the second where they could have been used to make organization better, and a third where they were not and should not be used.

Dontride.com

This website is very much under development, but still serves as a good example. On this site, bicycling hazards are collected and displayed on a custom Google map. I wanted to retain the use of pages for actual static pages on the site and posts for a future blog. Plus, doing so many custom loops having a custom post type to query made things much easier. In this case, each bicycling hazard is an entry in the “hazard” post type — a 3rd type of information requiring a discrete categorical-type taxonomy.

888artfest.com

Prior to WordPress 3.0 there was no easy way to create the UI for custom post types (and before 2.9 they weren’t an option at all). Unfortunately this site was developed right around the 2.9 release so in order to get the extensive listings of Artists, Authors, and Art Festivals pages and custom fields were pressed into service. While it works, I would completely re-think that scenario under WordPress 3.0. Artists, Authors, and Musicians would get their own custom post-type and Art Festivals would get another.

NessPlace.org

It may have been tempting (had this site been developed under WP 3.0) to use custom post types for each of the artists. Instead, I chose to re-purpose the authors model which makes it very easy to attribute pages (artwork) and posts (news) to a particular artist. I could have taken this one step further and created a custom user type (i.e. instead of author) to manage the exact capabilities I needed to give them. In this way, it would be much more flexible than custom post types could be.

How to implement Custom Post Types

There are a bunch of tutorials on how to implement custom post types. Rather than repeat the same information, I’m going to link to some of the better ones

  • WP-Engineer: This was actually written well before 3.0 was released, but it’s a very good article and was the first one I used.
  • Kovshenin: Once you’ve gotten the basic custom post type up and running, this article explains how to modify and extend the various settings to actually be able to use it.
  • WP Codex: It’s always good to go to the source, and the WP Codex has decent information the topic. I’d read the article about post types (linked above) as well as the primary function: register_post_type.

    Now that you know what custom post types are all about and have some ideas on when to use them and when to not use them: go forth and be evil.