Building a WordPress Blog People Want to Read, 2nd Edition

WordPress Custom Post Types and Taxonomies

Adapted from Building a WordPress Blog People Want to Read, 2nd Edition (Peachit Press)

By Scott McNulty



Read more Insight articles
Mediabistro On Demand design video tutorials. Request your two-week free trial.



Pages and posts are actually the same thing to WordPress. This news is shocking, I know, but a page is just one type of post. (A post is another post type.) In what follows I'll go over the default post types and show you how to take advantage of the custom post-type and taxonomy features of WordPress 3.0.

Types, Taxonomies, and Your Blog

Post types have been behind the scenes powering your blog from the beginning. An item’s post type determines how WordPress displays the information contained in that item. By default, WordPress has five post types, most of which you’ll recognize:

  • Post
  • Page
  • Attachment: Every time you upload a file using the media uploader, WordPress creates an attachment post type to store data about the uploaded file.
  • Revisions: This post type actually complements another post type (post).
  • Nav Menus: These posts contain all the information needed for the navigational elements of your blog.

As you can see, all the major elements of a WordPress blog depend on post types. What if you want to extend WordPress with additional post types, like one dedicated to books? Sure, you could create a Books category and just use plain old posts (which have a post type of post), but you can also create a new post type that will allow you more flexibility.

Custom Post Types
WordPress 2.9 introduced the idea of custom post types, allowing you to create a one-of-a-kind post type just for your purposes. The default WordPress post type includes a title field, which applies to books. But what if you want something a little more book-specific? Perhaps you don’t want to apply categories to a book post, but you do want to list genres and the author of the post. Creating a custom book post type allows you to decide what elements appear in the Add New Post/Edit Post screens and enables you to create custom organizational schemes, or taxonomies, which I cover in the next section.

Custom Taxonomies
As with post types, you’ve been using taxonomies without even knowing. (This makes you sort of like a fly from New Jersey that finds itself on a jet plane and ends up in France.) Categories and tags are two examples of taxonomies. In other words, a taxonomy is a system by which you identify and organize things, be they blog posts, animals, or currency.

When you create a custom post type like book, you have the option of creating a custom taxonomy for that post type. You can assign more than one taxonomy to any post type, including the five default types, if you like. Using custom taxonomies is a great way to separate out highly specific data that pertains to the custom post type but not to all the posts in your blog.

Moving forward with the book example, it stands to reason that you would want to include the book’s author in the post type and perhaps some other data, such as publishing company, genre, and edition type (hard-cover or soft-cover). You wouldn’t want to create a separate category for each author, and though you could use WordPress’ standard tag functionality, that solution isn’t very elegant. (Tags generally aren’t meant to contain such specific information, and wouldn’t it be nice if the author’s name wasn’t displayed with a mess of other tags?)

Types and Taxonomies Combined

These concepts are fairly difficult to convey without concrete examples, so I’ll just jump in and show you how to create a custom post type of book that will have a few custom taxonomies.

Creating a Custom Post Type
You can create a custom post type in two ways:

  • Edit the code of your blog directly.
  • Use a free plug-in to manage the whole process through a nice user interface.

I’m a fan of user interfaces, so I’m going to show you how to create as many custom post types as you like with the help of a plug-in called Custom Post Type UI.

Tip: If you’re a coding kind of person, check the WordPress Codex’s Custom Post Types page for instructions on how to add post types without using a plug-in.

After you’ve installed and activated the Custom Post Type UI plug-in, you’ll notice a new module in the navigation bar with some interesting options: Add New, Manage Post Types, and Manage Taxonomies.



The Custom Post Type plug-in’s options show up in your navigation bar.

Because you want to create a new post type called book, clicking Add New is the way to go. When you do, you’re taken to the Add New screen, where you can create both a new custom post type and custom taxonomies.

You need to create a custom post type before you can assign any custom taxonomies to it, so to start, you’ll concentrate on the left side of the Add New screen.



The Add New screen.

Setting post type options
Each custom post type needs a few pieces of information, which you enter in the Create New Custom Post Type half of the Add New screen:

  • Post Type Name. This required setting is the name of the post type you’re creating. Make sure to enter a name that won’t confl ict with another post type. (The names of custom post types can’t be repeated.) The name has be fewer than 20 characters long.
  • Label. The label of your custom post type is what that post type’s section will be called in the navigation bar. (Much like the post types of posts and pages, your custom posts will be displayed in the navigation bar.)
  • Singular Label. Most post types should be plural because they’re collections of similar items. Chances are that you’ll have more than one post of a given post type, but if you have only one post of that type, WordPress needs to know what label to display for it.
  • Description. In this text box, enter a brief description of the custom post type.

You’re going to create a custom post type of book, so fill out the form as shown below. If you change your mind later, you can click the Reset link at the top of the form to clear any values you’ve entered.



Creating a custom post type of book.

Clicking the View Advanced Options link displays a few more options that you can specify for your custom post type.



The advanced options give you much more control of your new custom post type.

You should keep most of the default settings, but the following two options will help you further customize your post type:

  • Menu Position. This option determines where in the navigation bar your custom post type module appears. If you want it to appear at the top of the navigation bar, enter 1 in the text box. If you don’t enter anything, your new custom post type will be listed at the bottom of the second navigation bar (the one that lists posts, pages, and the like).
  • Supports.This option is where you get the most bang for your buck in customizing your custom post type. Each of the items listed in the Supports section also appears in the New Post and New Page screens (as well as in their respective edit screens). If the box next to an item is checked, that item will appear in the edit and new screens of your custom post type. The book type doesn’t need to list an excerpt, trackbacks, custom fields, comments, revisions, or page attributes, so clear those check boxes.

When you’re happy with your settings, click the Create Custom Post Type button. The plug-in creates your custom post type while you wait. (Don’t worry; the wait isn’t long at all.)

When you refresh your browser, you should see a new entry in the navigation bar: Books (see below). The Books link takes you to a screen listing all the book posts you have on your blog (at the moment, none, because you just created the post type), and the Add Book link takes you to the Add New Book screen.



Your new custom post type (book) appears in the navigation bar like a native feature of WordPress.

Now click the Add Book link to check out the Add New Book screen (see below). This screen is a simplified version of the standard Add New Post screen, showing the items that you chose in the advanced options section of the Add New screen.



The Add New Book screen.

Just fill in the information as you normally would and click Publish to create your first post of your custom post type.

Understanding post-type permalinks
After you add a new post that has a custom post type, it doesn’t appear in your blog’s index. The default WordPress theme (like most all other themes) displays only posts with the post type of post. To have other post types appear in the index, you have to edit your theme.

The post you created does exist, however, and you can get to it even if it isn’t displayed on your main page. In the example in this chapter, you created a post with a custom type of book for the very book you’re reading. (Freaky, huh?) If you go into your custom post type’s edit screen (by clicking your custom post type in the navigation bar), you’ll see a button called View x, where x is whatever you’ve named your post type. For this example, the button is called View Book.



The View Book button takes you to your custom post.

Click that button to view your post, and make note of the URL, which will look like this:

http://yourwordpressURL.com/yourCustomPostTypeName/Title_of_Post

For the book post-type example, the URL of the post for this book would be

http://www.ipadbungalow.com/book/building-a-wordpress-blog-peoplewant-to-read/

As you can see, your custom post type’s name is important because it becomes part of the permalink for each post with that post type. Keep this in mind when you’re creating post types.

Creating a Custom Taxonomy for a Post Type
You have a custom post type of book, and now you want to be able to associate those posts with their authors and genre. This is a job for custom taxonomies.

In the Custom Post Type UI plug-in, click the Add New link in the navigation bar. When the Add New screen opens, you see the Create Custom Taxonomy section on the right side of the screen.

For this example, fill out the form as follows:

  • Taxonomy Name. Enter book_authors in this text box. You may wonder why I’m not having you call the taxonomy authors. I was tempted to, but because WordPress itself tracks authors, I thought it best to prefix the taxonomy name with the post type it will be describing — generally a good practice to avert any naming conflicts within WordPress.
  • Label. The label is what will be displayed in the post, so entering Authors is fine here.
  • Singular Label. For the single label, as you might expect, just enter Author.
  • Attach to Post Type. The final section, which is required, tells WordPress which post type should have this custom taxonomy available to it. You can choose more than one post type to make sure of your custom taxonomies, and you can even apply them to the stock post and page post types. In this case, select the Books check box to apply the taxonomy to the book post type — the type I just created.

The illustration below shows what your settings should look like at this point.



Creating a custom taxonomy with the Custom Post Type UI plug-in.

When all the options are set, click the Create Custom Taxonomy button. In addition to creating the Authors custom taxonomy, you just created a Genres taxonomy. The Books module in the navigation bar now lists both Authors and Genres links (see below). Clicking the link for your custom taxonomy (in this case, either Authors or Genres) takes you to the management screen for that taxonomy, which behaves just like the category-management screen.



The custom taxonomies Authors and Genres are now listed in the Books module.

Click Add Book (or whatever your custom post type is) to see the other way you can add entries to your custom taxonomies. Two new modules appear for the book custom post type: Authors and Genres (see below). Add entries to the custom taxonomies associated with your custom post types the way you’d add tags to a post: Enter as many terms as you like in the appropriate fi eld (Authors or Genres, in this case), separating terms with commas, and then click Add to add the new entries to the custom taxonomy.



Custom taxonomy fields appear in the new/edit screen of the post type they’re associated with.

When you view the post, your custom taxonomy entries don’t show up. This is a template issue, which I'll show you how to correct.

Note: Before you edit any theme, be sure to back up its files. Theme files are located in wordpress/themes (where wordpress is your WordPress directory). Look for the directory named after the theme you’re editing, and save a copy of that folder. Thereafter, if you mess up and want to revert to the original files, you’ll have them right there.

Select your theme in the Edit Themes page (for this example I'm using the default Twenty Ten theme) and then select the loop.php file in the Templates section. The Twenty Ten theme uses The Loop in an advanced way, so don’t be frightened by all the code you see. The first section of code is actually a little bit of documentation that tells you how the loop.php file works.

Scroll down in the editor until you see this code:

‹?php while ( have_posts() ) : the_post(); ?›

This line is the beginning of The Loop, which loops through all the content of your blog, specifying what content is displayed on the front page of your blog and how that content appears.

The first part, ‹?php, is just standard PHP code that isn’t specific to WordPress. The (have_posts() ) part is a function that queries your WordPress database to see whether you have any posts. If you do, it starts a while loop that iterates through each of your posts. Code is executed for each post in The Loop via several template tags, some of which work only within The Loop.

By default, the only post types that are included in The Loop are posts — that is, only blog posts are displayed on the front page of your blog. For this exercise, you want to include your custom post type book on the front page. To have The Loop include both the post and book post types, you have to insert a line of code before The Loop starts so that it looks like this:

‹?php query_posts( array( ‘post_type’ => array(‘book’ , ‘post’ ) ) ); ?›
‹?php while ( have_posts() ) : the_post(); ?›

By default, the query that gathers all the content to be used by The Loop includes only ‘post’ . Adding the name of a custom post type — in this case, ‘book’ — tells WordPress to include book post types on the front page of the blog.

Save the loop.php file by clicking the Update File button in the Edit Themes page, and reload your blog’s home page.

Some Uses for Custom Post Types and Taxonomies

By now, I hope your mind is buzzing with potential ways to leverage both of these features in your blog. If not, here are a couple of simple ways to use custom post types and taxonomies:

By now, I hope your mind is buzzing with potential ways to leverage both of these features in your blog. If not, here are a couple of simple ways to use custom post types and taxonomies:

  • Rudimentary store displays. Create a custom post type for each type of item (pants, shirts, what have you), and use custom taxonomies to list the sizes, prices, and colors available.
  • Photos. If you’re a photographer, you may want to post pictures and not deal with any of the other standard post types. Just create a custom post type of picture, and use a custom taxonomy to track exposure and other camera settings.
  • Events. If you hold classes or other events, you can create a custom post type of event to display them in your blog’s sidebar. The custom taxonomies could include the dates and locations of the events.

Don‘t miss the next Photoshop article on Graphics.com. Get the free Graphics.com newsletter in your mailbox each week. Click here to subscribe.

Adapted with permission from Building a WordPress Blog People Want to Read, 2nd Edition by Scott McNulty. Copyright © 2011 Used with permission of Pearson Education, Inc. and Peachpit Press.