What is a Post Type?
By default, WordPress uses 5 different ‘post types’ – data stored in the “wp_posts” table of your WordPress database. The following definitions are from the WordPress Codex:
A “post” in WordPress is the main type used by the blog. Posts are normally displayed in the blog in reverse sequential order by time (newest posts first). Posts are also used for creating the feeds.
A “page” in WordPress is like a post, but it lives outside the normal time based structure of posts. They have their own URLs directly off the main site URL. They can also use special Page Templates to display them. Pages can also be organized in a hierarchical structure, with Pages being parents to other Pages.
An “attachment” is a special post that holds information about files uploaded through the Media upload system. They hold all the description and name and other information about uploaded files. For images, this is also linked to metadata information about the size of the images and thumbnails generated from the images, the location of the files, and even information obtained from EXIF data embedded in the images.
A “revision” is used to hold draft posts as well as any past revisions of existing posts or pages. These are basically identical to the main post/page that they are for, but have that post/page as their parent.
The “nav_menu_item” type holds information about a single item in the Navigation Menu system. These are the first examples of entries in the posts table to be used for something other than an otherwise displayable content on the blog.
Therefore, a custom post type is simply the ability to create your own post type and customize the taxonomy, input fields, user interface, and how it is displayed on the front-end of your website.
The Old Way of Doing Things
At one point a few years ago, I found myself working with a theme for a client that used an image slider on the homepage. One of the client’s requests was that the image slider be easy to manipulate on the backend. “Of course it will be!” was my response. When I went to show him how to update and/or add new images, I had to walk him through creating a new blog post, specifying “slider” as the category, then adding a custom field name “featured_img_slide” with the image URL as the value for that field. As I explained this two him, I was met with a blank expression. I couldn’t blame him — it can be confusing explaining to clients that a slider is a blog post. The same applies to other sites I’ve built in the past — explaining to someone that in order to add new product listings, they have to add a blog post and categorize it as “product”, etc. When it came to displaying this info also, you had to create custom page templates that styled certain categories in a specific way and ran custom queries to output this information.
The Benefits of Custom Post Types
- Hide content from default queries – Custom post types allow you create content that isn’t lumped together with other posts, and therefore can be hidden from default queries. This comes in handy if you have something like product listings in addition to a blog on your site.
- Ability to Better Manage Content – By separating content, custom post types make it easier for you to manage your content. Products, Slides, Portfolio items, and whatever else you choose to define can all be stored in different areas. No longer do you have to sift through different categories of blog posts to segregate content.
- Improved Usability – By being able to control the user interface, you can make the the backend of the site much more user friendly. For example, if you want to define a custom post type as images for a slider on the homepage, you can have only the featured image upload enabled for that post type. You can eliminate unnecessary inputs or add additional inputs to control the user experience.
- Makes WordPress a True Content Management System – By not being able to define custom post types in the past, WordPress was limited as a content management system. The software was initially designed as blogging software, and evolved to resemble a content management system. Allowing users to define custom post types extends the functionality of WordPress and makes it a true content management system.
Creating a Custom Post Type
There are three ways to create a custom post type: use a plugin such as Custom Post Type UI plugin, create your own plugin, or register a new plugin in the functions.php theme file. I prefer to work with the functions.php theme file, because I don’t like to use or create plugins unless absolutely necessary.
Step 1: Register Post Type
The first step is to register your new post type. In this example, I’m going to register a new post type called ‘Portfolio’. The code that I use is as follows:
Here’s what’s going on here. The first thing I do is use add_action to hook into the initialization of WordPress and run the function ‘portfolio_register’. In the function portfolio_register, I first declare an array called $labels, which contains the labels for the custom post type. A full summary of labels and their uses can be found here. I then declare the arguments, a full list of which can be found in the WordPress Codex. Finally, I run the function ‘register_post_type’, which is passed two variables – the name of my new post type, and the $args that we declared prior to that.
Step 2: Register a Taxonomy
Just for fun, I’m going to register a taxonomy also, called ‘skills’. This will be useful if I want to organize portfolio items based on the skills that were involved in them, such as “wordpress”, “html”, “css”, “e-commerce”, etc. The code I insert into the functions.php file looks like this:
register_taxonomy(“skills”, array(“portfolio”), array(“hierarchical” => true, “label” => “Skills”, “singular_label” => “Skill”, “rewrite” => true));
You can read more about the anatomy of the register_taxonomy function here.
Step 3: Add Meta Boxes
In this example, I’m just going to add one meta box – “Year Completed”. I thought this would be important to show how you can customize the user interface. This example is very basic, and you can really go wild customizing inputs and fields — adding file attachments, logo icons, featured images, etc.
First we create a function admin_initialize_meta, and in it run the add_meta_box function. I’m not going to go into too much details about the syntax of these declarations – you can read more about the add_meta_box function here. Basically what we are doing is telling the add_meta_box function to run the ‘year_completed’ function (which is declared below it), and assigning the add_meta_box function to the ‘portfolio’ custom post type. The ‘year_completed’ function outputs the fields that we want, and specifies where to store the data in those fields (in the $year_completed variable). The last part of this code snippet adds an add_action hook that makes sure the year completed data is saved when the user updates the post.
Displaying Custom Post Types
There are several ways to display custom post types – here I’ll go over two. The first is to simply create a file name single-[post type name].php and place it in your theme folder. This will be the template used to displaying single post types. In this example, I created single-portfolio.php to display single portfolio items. The code I used in single-portfolio looks like this:
I use template tags to display the title and content, then pull the custom field info for year completed and display that. Also, I pull the featured image and resize it using the timthumb.php script.
The other way to display custom post types is to query the post type and run a loop to output that information. For example, on my homepage, I run a loop to output the featured images of the 12 most recent portfolio post types – the code is as follows:
Here is use the get_posts function to pull the posts using the arguments post_type, orderby, and numberposts. I then run a foreach loop because I am pulling data from an array – the featured images. I output the featured images one at a time, linking to that image’s permalink, and resize the image using the timthumb script.
Clearly the example listed above is only scratching the surface of what’s possible using custom post types. Check out Justin Tadlock’s forum plugin that uses custom post types to create a discussion forum. I recently read an article on Smashing Magazine about How To Use Custom Post Types to Organize Online Marketing Campaigns. Other possibilities include calendar events, real estate listings, and product listings, to name a few.
Here are some of my favorite resources for learning more about WordPress:
- WordPress for Beginners