There is an exciting upgrade to WordPress that makes it a more versatile content management system called custom post types. To better understand what a custom post type is, the most common example used is to create a post type for books. The post type would be book and each new post created would represent a different book. Also, in the admin panel there would a tab for this post type with the same function as current post and page tabs for adding, editing, categorizing, tagging, and deleting this post type. Sounds great right and the application seems limitless.
Background on What is a Post Type
In the older versions there was only a few post type that WordPress would use. Here is the breakdown with a description of each:
- Post – This represents the published content of a blog in the form of posts. Each row in the database assigned with this designation could be found under the posts tab in the admin.
- Page – This is the also represents published content within the site and each row in the database with this designation would be found in the admin under the page tab
- Attachment – This post type represents and file that has been uploaded to WordPress. A perfect example is an image you add to a post. This would be uploaded into the posts database and give the post type designation of attachment.
- Revision – This represents a older revision of a page or post. Every time you update a post or page a revision is created with the old data. This provides you a method to restore posts to their old content if needed. Post and page revisions are found on the edit post and edit page screens towards the bottom. Each revision shown here represents a row in the posts database.
- Draft/Auto Draft – These could be a few things. The first would be when you don’t actually publish a page or post and just hit save draft. The next would be a auto save that is done by WordPress as you are editing a post or page.
How the new Post Types Work
Seeing above that there was only a few was to class content, the new version allows you to create any post type that you want. As example, you can have post types that represent products, books, real estate listings, or whatever else you can think of.
When you create a new post type you will have the benefit of all the cool options on the post and page screen. Also, you will be able to create your own custom meta boxes to have custom areas of content. An example of this would be a field that you can enter the price of a product.
How to Use Custom Post Types
We basically have two options of where we want to create our new custom post type. The first would be in our function.php file in our theme and the second would be in a plugin file that we create. For this tutorial, we are just going to use our function.php file located in our active theme.
Next we are going to have to hook onto the initialization of WordPress and then use a function to register the post type. The frame of the code will look like this:
add_action("init", "add_my_post_type");
function add_my_post_type(){
register_post_type("my custom post type", $options_array);
}
Again we are using the add action function to hook onto the initialization process of WordPress and when WordPress initializes it will trigger our function to run to register our new custom post type. There are a few options that you should be familiar with:
- labels – This is an array that WordPress will use to apply names to the different inputs and names throughout the admin. So instead of add post, you would see “add book”. The way to assign what and where labels will be used by WordPress is by creating an associated array and using this in the options array. See the code below to see it in action.
- publicly_queryable - this is a boolean (true or false) that will indicate whether your custom post will be include in search results.
- query_var - this is a boolean as well that determines how these custom post will be designated in the URL of your site. So if you have a custom post type for products, to view a product you will need to put in the following URL: http://www.yoursite.com/product/your-product-name.
- rewrite – This is a boolean which tells wordpress if you are using a custom link structure like the one above.
- hierarchical – This option is a boolean. A true here will indicate that your custom post type can have parents and children. In this case they will be functioning like pages. False means that they will not have the ability to have parent and children.
- supports – This is an array of the type of meta boxes that you will have in your custom post type. The editor represents the content editor that you would find in the post or page edit or add pages. And so forth.
Here is it in action, the post type we have added is called “widget”.
add_action('init', 'my_widget_post_type_init');
function my_widget_post_type_init()
{
$labels = array(
'name' => _x('Widgets', 'post type general name'),
'singular_name' => _x('Widget', 'post type singular name'),
'add_new' => _x('Add Widget', 'book'),
'add_new_item' => __('Add New Widget'),
'edit_item' => __('Edit Widget'),
'new_item' => __('New Widget'),
'view_item' => __('View Widget'),
'search_items' => __('Search Widgets'),
'not_found' => __('No Widgets Found'),
'not_found_in_trash' => __('No Widgets found in Trash'),
'parent_item_colon' => ''
);
$options = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type('widget',$options);
}
As you will see this will add the tab in the admin for custom the custom post type widget.

So our custom post type exists and we can just start adding content, but we really should address the message that WordPress outputs when we add, edit, and delete a post (there are more). So we are going to use a filter hook to filter the WordPress messages to add our own to be used with our custom post type. Here is how we do it.
add_filter('post_updated_messages', 'widget_messages');
function widget_messages( $messages ) {
$messages['widget'] = array(
0 => '',
1 => sprintf( __('Widget updated. <a href="%s">View Widget</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Widget updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Widget restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Widget published. <a href="%s">View Widget</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Book saved.'),
8 => sprintf( __('Widget submitted. <a target="_blank" href="%s">Preview widget</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Widget scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Widget draft updated. <a target="_blank" href="%s">Preview widget</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
Now when you add a post you will see this:

You see some nice text that indicates that your custom post type has been modified depending on your action.
Now that you see how easy it is to you can start to add your own. In the future we will cover how to add custom meta boxes to the custom post pages. How to save these custom meta boxes and lots of other cool stuff like add JavaScript to your custom post pages to add some usability.