Justin Tadlock

Showing custom post types on your home/blog page

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)
  • album
  • movie
  • quote

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->is_main_query() )
		$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote' ) );

	return $query;
}

That’s all there is to it.

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() && $query->is_main_query() )

We’ll use the is_feed() conditional tag:

if ( ( is_home() && $query->is_main_query() ) || 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.

Like this tutorial? Please consider helping me write more in the future by making a donation via PayPal, grabbing something from my Amazon Wish List, or signing up at Theme Hybrid where you can ask me any support questions you want.