How to use Custom Post Types in WordPress 3.0
Since version 2.9, WordPress has introduced the ability to use custom post types. Now with version 3.0, things are taken a bit further with the option of creating panels for your custom post types. In this tutorial, we will show you how to implement Custom Post types in your site into your WordPress site.
Before we begin make sure you are running on WordPress 3.0 (Beta versions for now).
Watch the Screencast
Creating Custom Post Types – Using Plugins
As of version 3.0, WordPress does not have any built-in UI (user-interface) to create custom post types. There are only two options that we can use to create custom post types: plugins or to hard code them into your theme’s functions.php file. First, lets look over how we can use plugins to create custom post types.
Custom Post Type UI

Custom Post Type UI is a plugin developed by Brad Williams of WebDevStudios which allows you to easily create custom post types and taxonomies. One of the coolest feature of this plugin is that it generates a code to create custom post types, so you can then paste it into your theme’s functions.php file. One of the quirks of this plugin is the inability to share taxonomies among all your post_types.

From the Custom Post Type UI panel click on “Add New”.

Next you are given a few options to fill in. The “Post Type Name” is what will be used by WordPress to query all the posts from such post_type. The “Label” is what will be displayed on the sidebar of your Dashboard, just like the regular “Post” menu is. If you expand the “View Advanced Options” you’ll see a few more options that you can configure. Most are self explanatory, such as “Public” and “Show UI”. The first one when set to true allows the custom post type menu to be displayed on the sidebar, and the other one (show ui) when set to true generates the menu panel.
“Rewrite” is what allows the custom post type to use SEO Friendly WordPress URLs (Permalinks). The “Custom Rewrite Slug” can be set to anything that you like. WordPress will use this slug to generate the permalinks. So if we have example.com with a custom rewrite slug of “movies” your custom post type permalink would look like example.com/movies
WordPress “Query Var” function allows you to query your custom post type’s post. So if we used the example given earlier, to access a post with the tittle, My First Movie Post, which is written under the Movies post_type, we can enter example.com/?movies=my-first-movie-post. So the query variable looks like this: ?posttypename
Finally you can choose the different features supported by your custom post type, such as thumbnails/featured image, and excerpts.

CMS Press

CMS Press is another plugin that allows you to add custom post types and taxonomies, developed by Michael Pretty. One of the best feature of this plugin is the ability to share taxonomies between all the post_types.
To create a custom post type with CMS Press is pretty straight forward, just like we did with Custom Post Type UI. The two major differences is the “Content Type Identifier” and the “Permalink Structure”. The “Content Type Identifier” is simply the slug name you want to give to your custom post type. The “Content Type Identifier” then gives %identifier% the value of the slug name you have chosen to create the permalink. This feature allows you to set a different permalink structure for each of your custom post types.

So if you want your custom post type permalink to look like example.com/category/post_type/postname You’ll enter the following under the “Content Type Identifier”: %category%/%identifier%/%postname%
Creating Custom Post Types – Using Functions.php file

If you prefer to use custom post types without a plugin, then just add the following code to your theme’s functions.php file:
// Creates Movies post type
register_post_type('movies', array(
'label' => 'Movies',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'movies'),
'query_var' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes',)
) );
Lets dissect the code.
register_post_type( $post_type, $args ): This function accepts two parameters, $post_type or the name of the post type, and $args, an array of arguments.
label: Plural name given to the post type which is displayed in the admin panel sidebar.
public: true/false. Allows the admin UI to be populated with posts of this type.
show_ui: true/false. Shows or hides a default UI to mange this post type.
capability_type: Default: post Post type to use for checking read, edit, and delete capabilities.
hierarchical: Whether the post type is hierarchical.
rewrite: true/false. Default: true If slug argument is entered then the slug name is prepended to the posts.
query_var: true/false Sets the post type name as a query variable.
supports: Default: title and author Sets different support features the post type allows.
Visit the WordPress Codex for more info on register_post_type().
Displaying Custom Post Type Posts
To display the posts from your custom post type, add the following codes in the loop. Replace “name” with the name of your post type. Note: You don’t have to add the custom post types in your index.php file. You can create a Custom WordPress page and run the following query within the Loop.
query_posts( 'post_type=name');
To display posts from more than one post type, change the above code to the following. Change movies with your custom post type name.
query_posts(array('post_type' => array('post', 'movies')));
The above code will display all the post from the regular post type (post) and from the custom post type, movies.
That’s it. We hope this tutorial has been helpful and don’t forget to post any questions in the comments.
Comments
28 Responses to “How to use Custom Post Types in WordPress 3.0”Share Your Opinions
Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!
Please make sure that you have read our Comment Policy.











This is awesome article
I’m happy that WordPress has integrated custom post types right into the wp framework. However I’m not seeing how it is any better than using plugins already available such as Magic Fields or Flutter. With those plugins you get the same effect plus easy to setup custom fields with lots of different types and flexibility in how to enter and display your data. Does wp 3.0 support anything like that right out of the gate?
You can create a UI for custom fields and just about anything in the backend with Custom Post Types. You are right that those plugins make it very easy for users, but if any of those plugins fall on development, then you are left with no choice.
Fair enough. I’ve played around with the beta a bit, but not as much as I should I suppose. It would be exciting if it did go more that direction. Since I’ve become experienced using those plugins I almost don’t make a site now with one
So that would be great if all those tools were available and handled within the original application. I can see it leading to better backend management and encourage more people to contribute to it rather than a smaller group supporting a particular plugin.
I think they have intentionally left some of the custom post type code as code. To allow the average user to have to deal with it.
Great article, looking forward to Wp3 =)
Very nice article. The Custom Post Type UI plugin has the options very similar to raw PHP code of registering post types, that is great for developers.
Thanks for introducing many useful plugin to work with custom post types.
Just watched the video…
Is it true that we need to edit php files (= hard code) to get the whole thing working?
No, you have to do one or the other. In our video, we showed both ways. Either you can do the hard codes way, or the plugin way.
Allright, I see, thnx!
Then I suppose you started the hard code way? At the time seeing all the coding stuff only, I just quit. Therefore didn’t see the plugin way I guess…
Will this work on WordPress 2.9 or is it only for 3.0?
I tried using 3.0, but some quirk happened where none of my plugins would activate, even the ones that said they activated. Was very strange!
-Kevin
This would work with 3.0, so use it on 3.0 only. Try reinstalling WordPress 3.0, we are running a few sites on it.
I can’t help but think that this would have been more useful if you showed something out of the ordinary with post types, rather than use the old “movies and books” bit.
How about a sideblog using post types, or something a bit more practical? All this does is re-state what the WP Codex will eventually have, if it isn’t already on there.
We are sorry that you feel this way Chris. WP Codex will not have a video that will show you how to do this. It does not matter what names we use for the custom post types, the idea is how to add them. We will not create an extra-ordinary site just to write a post about custom post type. If you seek that knowledge, you are probably better off doing it yourself. This blog still has to fulfill the needs of the beginner level users hence why we shared the plugin method.
Can you go one step back: Why do I need custom post types in the first place? What’s the use-case?
Custom Post Types are good for users who are using WordPress for more than a Blog, for example CMS. Lets say if you want to have a site that has your portfolio and your blog. Obviously you don’t want your blogs to look the same way as your portfolio does. That is when custom post types comes in handy. This is a very versatile feature for taking WordPress to the next level. For average blog user, they probably don’t need custom post types.
Hi, That was a great article. I was hoping that you may know the answer to my question.
In the last couple of lines you have:
this returns all posts and post_types named movies.
Do you know how to return both post_types named ‘movies’ and posts in the category ‘movies’ but exclude all other posts that do not have this category??
You would use the category parameters for the query_posts – http://codex.wordpress.org/Function_Reference/query_posts
Any easy to understand guides on how to add options to the custom posts? For example, if a movie is drama, comedy, horror…. to have those options as check boxes WITHIN the custom post edit panel? I can’t seem to find a guide or plug-in for that.
Thanks for this great article! I’m a real wp beginner, and maybe you can help me with this question:
Is it possible to display archives of one custom post type in the sidebar like one can do by using the “display archives by cat”-plugin by kwebble?
Yes it is possible to display custom post types as archives. Just create a custom loop in the sidebar to display posts or categories from a specific post type.
Im very excited about this new feature ^-^ You made a nice video. Altough in your example, why use custom post types and not just make up a category books and movies for the posts? That way you save all the hassle of a custom post type.
First, it lets you organize things differently. Second, you can have a completely different write panel with different options. It allows for much more customizations…
I have tried to create a tag for custom field, and when I use get_the_tags within the loop of custom type, it doesn’t print anything.
Any suggestions?
I cannot seem to get the loop to work to query posts of my custom type. Your example seems easy enough so I tried the following $var = query_posts( ‘post_type=sponsor’);
Then I started a loop
while ($var ->have_posts()) : $var ->the_post();
Do stuff
endwhile;
this returns a php error:
Fatal error: Call to a member function have_posts() on a non-object in single-sponsor.php on line 22
line 22 is the line with the while loop.
Ideas? What am I doing wrong?
How do I get each content type to show up in the loop? I mean, it’s almost like Tumblr. If I post a picture… I need to customize the code in the loop – same thing I post a link.
You can see in the post, there is an option to show both a custom post types, and posts inside one loop…
Good tutorial, thank you for sharing.
I have a question on this.
I made a custom post type through functions.php (a calendar) which has a custom meta box in the admin UI. This meta box uses jQuery on one field.(http://jqueryui.com/demos/datepicker/).
For this datepicker to work, I need to embed the required scripts.
add_action(‘admin_enqueue_scripts’,'enqueue_my_scripts’);
works but my scripts get loaded on the entire admin UI. (which interferes with the default WP admin jquery)
So what I need is the hook for my custom post type.
this:
add_action(‘register_post_type’,'enqueue_my_scripts’);
doesn’t work because register_post_type isn’t a hook.
So, what is the hook for a custom post_type?