Custom Post Types in WordPress are in. Instead of reorganizing your site via categories for the default WordPress post type, you can know create your own. This allows WordPress to become a truly full featured CMS while still having the access of a simple blog. Of course, there are always two main methods of creating custom post types, hard coding them or adding them via a plugin.
Hard Coding a Custom Post Type
As an example, I am going to create a custom post type for News Items…
register_post_type('news', array( 'label' => 'News Items','description' => 'Admin portal to display news and events updates. Only the admin will see this post type.','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => ''),'query_var' => true,'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),'taxonomies' => array('category','post_tag',),'labels' => array (
'name' => 'News Items',
'singular_name' => 'News Item',
'menu_name' => 'News Items',
'add_new' => 'Add News Item',
'add_new_item' => 'Add New News Item',
'edit' => 'Edit',
'edit_item' => 'Edit News Item',
'new_item' => 'New News Item',
'view' => 'View News Item',
'view_item' => 'View News Item',
'search_items' => 'Search News Items',
'not_found' => 'No News Items Found',
'not_found_in_trash' => 'No News Items Found in Trash',
'parent' => 'Parent News Item',
),) );
Using a Plugin to Manage Custom Post Types
The best plugin to manage custom post types is the Custom Post Type UI. This plugin is extremely powerful. Not only does it let you add custom post types, but it also allows you to create and manage custom pages and taxonomies (categories and tags). All of this is done in a nice user-interface (thus the UI in the name) that is found at the bottom of your left-sided admin navigation panel.
FAQ
So my custom posts do not seem to show up? How can I get custom posts/pages to include themselves in the main loop?
It is true, by default WordPress does not include custom post types within the loop. This makes the asides pages for custom post types display no content, yet going to the page still works. There are many plugins to accomplish this task quickly. I prefer the Simple Custom Post Type Archives plugin. Install and everything works instantly.
I have installed a plugin that adds a custom post type that is not included under the “Manage Custom Posts” with the Custom Post Types UI plugin. How do I find post type names for custom posts?
-
With Custom Post Types UI
- Go the the plugins drop down menu on the left-sided admin navigation bar.
- Here, you can see all custom post types created only by this plugin. Find the column entitled, “Name”.
- Scroll down to the post type you are looking for. This entry is the actual post name, identifier.
-
Finding other Custom Post Type Names
- Install FireBug
- Open your site and go to a custom post type page.
- Right click on the content area and select the menu option, “Inspect Element”.
- Find the div that encloses one single post on that page. This div usually has an ID called post-#, where ‘#’ is the number of the post.
- In this div, look for the class called “type”. After the first hyphen of this class (so after “type-”) is the type name of this post. The default post type is “post”.
If you have created a custom post type with Custom Post Types UI, then finding the post type is simple.
I recommend using FireBug for Firefox to acomplish this goal quickly. You can simply “View Page Source”, but FireBug is always quicker.
So I have the post type… but what can I do with it?
Now you can customize the loop to display custom post types differently. You can add conditionals to check if the current post is of a certain post type. From here the possibilities are endless. Below I will continue to use the example from above with the custom post type of “News”.
<?php if('news' == get_post_type()) : ?>
<?php else : ?>
<?php endif; ?>
If you need a conditional that can check for a custom post type outside of the loop.php, then you will need a slightly different version of this code.
<?php
global $post;
if( get_post_type() == 'news' ) : ?>
<?php else : ?>
<?php endif; ?>
Let’s say I want to change the way a custom post looks on the archive (multiple posts) compared to that of a single post page without altering all single pages or custom post pages on my site?
In plain English, if the current post is of “this” post type and it is a single post page, then do some stuff. Code below…
<?php
if( get_post_type() == 'news' && is_single()) : ?>
<?php else : ?>
<?php endif; ?>
How do I get custom post types to display on my homepage?
Just place the following code in your functions.php file.
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() )
$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'attachment' ) );
return $query;
}
Please feel free to ask questions in the comments section below. I’d love to solve your codding problems!
Custom Post Type UI Support Page
WordPress Codex Conditional Tags
Creating a loop with Custom Post Types outside of the loop.php file (Scroll to the bottom).

Hi Chris
Thank you for this very helpful post! Trying to get to grips with Custom Post Types. I am using a theme with a slider on the homepage and waned to create a custom post specific to feed the slider only. Where do I find the code and how do I change it? Thanks again. Ivano
Thanks for the post Ivano, glad you liked it!
Your problem depends on a few things… Are you using a theme that has a slider built in, or are you using a plugin with a widget area that contains the slider?
If it is a custom theme with a slider, I would assume it might be in the header.php or index.php. Look for some code that has “wp_query” using control or command + F will prompt a search (depends on PC or mac). Let me know the details for the above and I’ll take it from there. I hope this puts you in the right direction!
Brilliant. Found it on an include in index.php named slider.php… The wp-query code is below. What do I change to have only my custom post type ‘slider-images’ show? Thanks very much. Ivano
have_posts()) : $featured->the_post(); ?>
<a href="”> <a href="”><a href="”>
<img src="/thumb.php?src=&w=115&h=94&zc=1&q=100″ alt=”">
Ok, I’m not on a good computer to test properly, but try these options…
< ?php
global $post;
if( get_post_type() == 'news' ) : ?>
< --THE LOOP CODE GOES IN HERE!-->
< ?php endif; ?>
The query may be looking for posts with the category of “Featured”, but i’m not 100% sure. I would need to see that entire file and possibly your functions.php file to really understand what is going on. These points will at least put you in the right direction. If you still can’t get it to work, feel free to e-mail me.
Also, your slider may be doing something like this example (Go down to the section entitled “Multiple Loops in Action” and look in the first code box). This example pulls a post from the category, “Featured”.
Any idea how to set the default category for a custom post type?
Or for each custom post type if there are several custom post types, and each one has more than one category?
Try something like this…
function add_sketchbook_category_automatically($post_ID) {global $wpdb;
if(!has_term('','category',$post_ID)){
$cat = array(4);
wp_set_object_terms($post_ID, $cat, 'category');
}
}
add_action('publish_sketchbook', 'add_sketchbook_category_automatically');
Place at the end of your functions.php file.
For more info check out these links,
http://wordpress.org/support/topic/set-category-to-a-custom-post-type-automatically
http://codex.wordpress.org/Function_Reference/wp_set_object_terms
Thanks for the reply. I had already seen the support link above. I haven’t tried it yet. Have you?
However, I did come across the plugin “Default Custom Post Type Categories”, available in the WordPress plugins area, and it works perfectly.
I did something similar in the past. I know wp_set_object_terms() is the key. Should work.
The code suggested cannot be implemented by the client, unless it is implemented in a plugin.
Will the code run immediately when a new custom post is published if a category is not selected?
The “Default Custom Post Type Categories” plugin allows the client to easily set the default category in an edit screen in he Admin, and that selected default category is automatically selected (checked) when you open the Add New CPT screen.