Custom Post Types in WordPress 3.0

I know it’s too early to speak about WordPress 3.0, but I’ve been playing around with some of it’s enhancements the other week, and.. Well, this is a miracle, really. Remember all those plugins promising custom post types in versions prior to 3.0. Those that never get updated and show themselves glitchy sometimes, especially during upgrades? Well the time has come.

In version 3.0 the developers of WordPress introduce the custom post types. I’m not sure whether it’ll be solely built into the API or also displayed as a GUI somewhere in the settings, but it doesn’t require too much coding skills to add a couple via your functions.php or a perhaps a plugin.

Well, first I’d like to share a few thoughts of what could be accomplished using these custom post types, and I think that some of the first ones to appear would be

  • Photo Posts
  • Quotes
  • Chats
  • Audio/Podcasts
  • and of course Video

These are basically the things that you’re allowed to post at Tumblr, but hey? We could already post these at the standard Posts type. Exactly, we could! But the custom types perhaps is a step to more organization, rather than functionality.

Why would we want to play around with custom fields, or add stupid meta boxes in the Edit Posts page and then teach our clients and/or content managers to use them? Why not just get rid of all those stuff and have them seperately in your main menu, and the meta boxes are customized to match the exact needs of certain post types.

Custom Post Types in WordPress 3.0

For instance, why should the Edit Post page contain the ability to attach an audio file if it won’t be used 50% of the time? Or why would Quotes contain post revisions? Chats and Video posts wouldn’t require image posting, right? This way you can talk to your client or content manager in a certain way:

John, could you please add a new Podcast? Here’s the MP3 file.

Instead of

John, I need you to add this MP3 file as a Podcast – please add a new post, then in the box on the right you should pick that your post type is a Podcast, then a little bit lower in the next box you should upload this audio file, and also please make sure that you check the Podcasts category before you publish.

No, John, don’t touch the Video box, we use that for video posts. No, leave the custom fields as they are, you should never touch that area

See what I mean? Manipulating also comes in clean and handy. You don’t have to sort different post types into different categories, then use the Edit Posts screen and set filters to a certain category to get a list of Podcasts. Wow.. Also, once your post is a Podcast, it doesn’t hurt your eyes in the Edit Posts section, it’s not visible there at all. Once a Podcast – always a Podcast ;) Awesome isn’t it?

More awesome is the way you can customize the columns in the Edit screen to fit your needs, for each and every post type! Take a look at this Podcasts Edit screen example:

Custom Post Types in WordPress 3.0

Now isn’t that awesome? I bet it is!

So what else could be done with WordPress’ Custom Post Types? Well, basically anything. Say you run an online store which of course has some static Pages (such as Contact, About, etc), some blog Posts, cause we’re so 2.0, remember? And Products, which would be a custom post type that contains the product name, description, product price, stock availability, and could even contain inquiries in forms of user comments!

One more example – a Real Estate Agency, same story – Pages, Blog Posts, News stories, property For Sale, property For Rent, Land for sale. The last three would contain extra taxonomy in forms of Country, Region. Custom fields such as price, total area, etc. The Edit Property page could even contain a Google Map where you could point out its location! Here’s a screenshot:

Custom Post Types in WordPress 3.0

Amazing!

Using Custom Post Types

Yeah, all this is good, but it gets even better as soon as you find out how easy it is to define (register) Custom Post Types in WordPress 3.0! Err.. This means that you have to actually switch to 3.0 before any of this works. Read the article on Nightly Builds, switch to a 3.0-dev branch and launch an upgrade. You should be able to get 3.0-alpha by now.

So let’s try to add a Podcasts type and let’s go straight to the code:

// Fire this during init
register_post_type('podcast', array(
	'label' => __('Podcasts'),
	'singular_label' => __('Podcast'),
	'public' => true,
	'show_ui' => true,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => false,
	'query_var' => false,
	'supports' => array('title', 'editor', 'author')
));

Oh my god! Is that it? Yes! I wouldn’t like to go through the parameters but you could read about them in the WordPress Codex – Function Reference: register_post_type.

Perhaps the tricky part would be with the custom columns? Nah:

add_action("manage_posts_custom_column", "my_custom_columns");
add_filter("manage_edit-podcast_columns", "my_podcast_columns");

function my_podcast_columns($columns)
{
	$columns = array(
		"cb" => "<input type=""checkbox"" />",
		"title" => "Podcast Title",
		"description" => "Description",
		"length" => "Length",
		"speakers" => "Speakers",
		"comments" => 'Comments'
	);
	return $columns;
}

function my_custom_columns($column)
{
	global $post;
	if ("ID" == $column) echo $post->ID;
	elseif ("description" == $column) echo $post->post_content;
	elseif ("length" == $column) echo "63:50";
	elseif ("speakers" == $column) echo "Joel Spolsky";
}

Whoot! All done. As seen in the code above, there’s an action and a filter involved. The action outputs custom columns depending on the type, while the filter simply defines the columns for the Podcasts post type. It’s as simple as that. Note that I trimmed the code a little bit to fit on screen, so you shouldn’t be simply outputing 63:50, but actually count the podcast length ;)

That’s about it, folks! Cheers, and say, What custom post types are you thinking of implementing in the first place? Please share via the comments. Thanks!

Follow up: Extending Custom Post Types in WordPress 3.0

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

392 comments