In the last few days, I’ve shown a few people a screenshot of something I’ve been working on for this site. The screenshot is of a home page displaying custom post types and not just the post post type. After numerous requests for the code to do this, I figured it’d be much easier to share it here.
WordPress version 3.0 will make creating custom post types extremely simple. But, the techniques I’ll point out in this tutorial can be used with previous versions of WordPress.
Changing the post type on the home page
By default, WordPress shows the post post type on your home page. Let’s suppose we want to show several post types in addition to posts:
page(yes, regular pages)albummoviequote
To add these, open your theme’s functions.php file and paste this PHP code into it:
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;
}
That’s all there is to it.
You may have noticed the addition of the attachment post type even though I didn’t mention adding it. This is something I highly recommend adding if you’ll be showing any attachment images on your home page For example, if you have a gallery on your home page, the images won’t appear without adding the attachment post type.
Showing the post types in your feed
Realizing that many of you might want to also add these post types to your feed to match your blog, a small change in the code is required. All you need to do is change this line:
if ( is_home() )
We’ll use the is_feed() conditional tag:
if ( is_home() || is_feed() )
Now, you can have custom post types in your regular blog post rotation and your feed. Enjoy and look for more custom post type tutorials soon.



One thing I don’t really understand is how/where I select what post type I’m about to write in the backend. Particularly when we don’t have a UI for that yet.
I believe that the UI stuff is what’s coming in 3.0.
If you’re using the trunk version you can see the UI, but you have to register your post type first.
Just replace “movies” with whatever you want.
Ok. This makes sense. So there is no real way of using custom post types with 2.9.1
You can use them, but you’d have to do all the admin UI stuff and rewrite rules yourself. Basically, it’s too much of a pain to do.
I’ll be writing a complete tutorial on post types in 3.0 as soon as everything is ironed out with them in trunk.
As Justin says, in addition to the code you still have to do the UI panel. Right now WP 3.0 trunk doesn’t display any UI even after registering post types.
I got this to work in the current trunk including the UI panel with a couple of simple additions to you code.
function post_type_movies()
{
register_post_type('movies', array('label'=>__('Movies'), 'public'=>true, 'show_ui'=>true, 'exclude_from_search'=>false));
register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');
Wow.. great features, Can’t wait for WP 3.0
Great example! I’ve never used $query->set() before. I believe that you just opened up a whole new world for me. Thanks!
I actually end up using it a lot, at least in my support forums. So many people want to get rid of a category or just show a specific category on the home page. Of course, most tutorials around the Web tell them to use
query_posts()even though there’s a perfect filter hook for the job that keeps users out of theme templates.Can you talk more about how you use query set? I’m not sure I’m understanding how this keeps people out of the templates any more than query posts.
Very useful information. Especially for a newbie like me. I will try to implement that on my blog. Thanks.
Wordpress 3.0 is coming? I don’t think I’ll upgrade that quickly when it comes out, maybe I’ll have to wait it out till 3.1 comes out.
It’ll be a while, but why would you wait? There’s some exciting things coming in 3.0.
Cool feature, but i think is better wait for the 3.0 version of WP, im not confident enough on messing on installation files.
I’m not sure if you misunderstood something from the post, but I’d never advocate changing installation files.
The tutorial deals with editing your theme’s
functions.php. The methods used above work in 2.9 and will be the same in 3.0.Hi, another use from this wordpress 3.0 . Very thankful to you for sharing.
I will try this one as soon as i get WP 3.0. A great stuff to help the bloggers. Really i need the code for that and you have provided it.
I can’t wait for Wordpress 3.0! I’ll be waiting for your tutorial.
[...] Justin had been playing with these types, we check out the possibilities of types for [...]
Am I correct to assume that 3.0 will introduce new admin panels for custom post_types?
So in 3.0 if I create a custom post type ‘movies’ I will then have a new ‘movies’ choice in the admin panel where the user could input the information relevant to ‘movies’ only?
Right now with 2.9 I basically create new meta field boxes for the admin to fill in while writing a post. The problem comes into effect when I have ‘movies’ as a type of post, and say ‘music’ as a second type.
Right now in 2.9 both the meta input boxes show up in the ‘add article’ admin section, which makes it really confusing to the user. They have the ability to input information both for movies and music on the same ‘add article’ admin page.
This would indeed go a long ways into turning wp into more of a cms, where the author has a much clearer path as to where to input the data for each type of post…. boy I sure hope I am right.
How do custom posts jive with traditional taxonomies, such as categories and tags? If I queried a category, would all posts and (musics, et al) that have that category display?
I’m interested in this, I heard that they were adding the Categories and Tags taxonomies to Pages so, at least in my mind, it makes sense that they would apply to both. And I’m assuming that custom-post types would also be applicable, as long as you made sure they were registered for those taxonomies.
Which, if that is true then it makes sense that any custom taxonomies could also be registered across multiple custom post-types, as well as your posts and pages, if you desired as well..
Very exciting stuff.
nice feature, this is very useful for me.
[...] qui permet d’attribuer un genre à un article. Cette fonction est reprise en détail par Justin Tadlock. Ensuite, WordPress 3.0 autorisera la personnalisation simplifiée des arrières-plans de nos sites [...]
[...] goes to Justin Tadlock for this handy [...]
Hi,
As you wrote, if I remove the ‘attachment’ type I’ll get the posts without the images. I would like to get the images without the posts but when I omit the ‘post’ type’, and leave onle the ‘attachment’ type, I get nothing.
Any idea?
I’ve just been playing around with this in WP 3.0 svn and so far so good. I can easily create a new post type, assign the default post features and custom taxonomies and get it all in a custom write panel. It’s also easy to create custom meta boxes linked to the new post type.
What I’m wondering about is:
1. How will adding custom fields to the custom post type be managed (one could just use the post custom fields but what if it’s a more complex post type that warrants its own DB table.)
2. I’d like to see an interface to also be able to add arbitrary custom fields to the Quick Edit part on the post list.
I guess what I want to be able to do is create custom front end functionality depending on the post type. For example:
Event Post Type
- display additional fields like event date, duration, venue, contact, etc…
- change the post sorting to use event start date or venue or any other additional field.
So basically my front end template will have different functionality than the default post type of ‘post’…
I guess I’m jumping ahead a bit here but just wanted to voice my thoughts
.
wow great post thanks for sharing with us
nice feature!
[...] make sure to check out what Justin Tadlock has to say on this [...]
Totally helpful. Thanks a lot
I’ve problem..How do custom posts such as categories and tags?
[...] Showing custom post types on your home/blog page [...]
Awesome tutorial, but what would I have to do if I wanted to query only one specific type in the sidebar? I tried your Query Posts widget to query all the items from a non-hierarchical category “song” and it’s impossible.
And how can I assure that a custom post type will be page-like? I was trying to get rid of some things in the display on “song” post type and make it output values of certain custom fields by making it a custom template and I realised that it’s not possible, because it’s post-like, not page-like. Would creating a template file song.php result in anything by default? I thought this is what add_post_type_support() could do but I have not seen any references to that yet.
I guess that I can’t wait for the rest of this series, as so many things are left unsaid and your and WP Engineer’s posts on this both left everyone curious.
We are going to kick-off a blog for our company. And, I’m curious as to why you chose WordPress over say TypePad or Blogger. What’s the advantage?
[...] Showing custom post types on your home/blog page [...]
does this work with xampp? because i am using one i don’t if it will work.
Most usefull post, but still can’t wait for wp3.0
I also agree with Jason Pelker, WP will become defacto CMS and still be easy to host it anywhere.
This is very helpful. Thank you very much. Are you going to write a bit more about it?
One thing I noticed with using the Wordpress dev version is that the edit post/page title has changed. It probaby is constructed something like this: “Edit $post_type”.
This is bad, because it doesn’t take other more complex languages into account.
For example, both finnish and estonian have over a dozen of different cases, and this is one of the places where they are used.
For example, while editing a page, in estonian this would display “Muuda leht”, whereas it should be “Muuda lehte”.
Nice, can’t wait for your full tutorial when 3.0 is released.
I feel these will evolve into complete custom write panels, image the possibilities.
It’s difficult to be patient for all the features coming in 3.0! It’ll likely be the most revolutionary WordPress version yet and in all truth, it will subsequently become the defacto CMS throughout the world.