Practical Use Case for Custom Post Types

I just thought I would share a snippet in my most recent project where we have used the new custom post type feature available in WordPress 3.0.

Just a quick refresher on what custom post types are: A custom post type could probably be better termed as a “custom content type” because it doesn’t have to be a “post” at all. In fact if you are creating these for blog posts you’re probably using them wrong and you would be better served to just organize your content by category or tag or another custom taxonomy.

In WordPress there are two major types of content that most sites use – pages and posts. Having these two different types of content allow the developer to easily separate out different types of content. Adding in a custom post type allows the developer to separate out even more content. So before what usually would have been organized by a category or tag can now be its own separate entity.

So for this article we have a site where the client is utilizing pages for main content areas, they have a blog where they will be posting news and articles and they would also like to be able to put client quotes on the site. Before WordPress 3 I most likely would have set up the site to have a category called “quotes” and would have instructed to the client to not create their blog posts in the posts section but also their client quotes. While this isn’t a big deal if we can keep the blog posts and the quotes separate it makes it that much easier on the client and keeps all the quotes out of the default rss feed, site wide search, and archives.

So how do we do it?

add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
	register_post_type( 'client-quotes',
		array(
			'labels' => array(
				'name' => __( 'Client Quotes' ),
				'singular_name' => __( 'Client Quote' ),
				'add_new' => __( 'Add New' ),
				'add_new_item' => __( 'Add New Client Quote' ),
				'edit' => __( 'Edit' ),
				'edit_item' => __( 'Edit Client Quote' ),
				'new_item' => __( 'New Client Quote' ),
				'view' => __( 'View Client Quote' ),
				'view_item' => __( 'View Client Quote' ),
				'search_items' => __( 'Search Client Quotes' ),
				'not_found' => __( 'No client quotes found' ),
				'not_found_in_trash' => __( 'No client quotes found in Trash' ),
				'parent' => __( 'Parent Client Quote' ),
			),
			'public' => true,
			'exclude_from_search' => true,
			'menu_position' => 20,
			'menu_icon' => get_stylesheet_directory_uri() . '/images/customer-quotes-icon.png',
			'query_var' => true
		)
	);
}

I won’t go through what every bit of code means. For that check out Justin Tadlock’s comprehensive article on custom post types.

A couple things I would like to point out however I believe are essential to giving your client a good experience with custom post types, because these are supposed to make content management easier right?

First, label everything. You don’t want your client clicking the custom post type menu item and then having the page header say “add new post.” That would confuse the heck out of them.

Second, give your custom post type and nice icon that matches with the rest of the admin icons. You can see the screen shot below for this particular use case.

After we have the quotes entered in, now it is a simple query to extract them.

'client-quotes', 'posts_per_page' => 10 ) ); ?> have_posts() ) : $loop->the_post(); ?>

And to finish it off, I put these all in a jQuery slideshow so that they the rotate nicely on the page.

Comments

  1. A. says:

    That’s a great explanation of things, what I was wandering is how would you tackle the “view order” issue of those “custom post contents”?

    say the client has a 100 T-Shirts as custom post type SHIRT and he wants to set their appearance order, how we he do it?

    Thanks!

  2. Great way to integrate customer quotes into a client site. I build small business websites, and they all need a testimonials section. Keep up the great work, and thanks for sharing this information.

  3. I’ve been doing quite a bit of reading on using ‘custom post types’ and this write up is one of the easier ones to follow.

Speak Your Mind

*