
WordPress 3.0 will allow you to create custom post types, so what about being able to list those custom types on your blog homepage? This very useful piece of code will show you how you can do it.

WordPress 3.0 will allow you to create custom post types, so what about being able to list those custom types on your blog homepage? This very useful piece of code will show you how you can do it.
The following code have to be pasted in your functions.php file. Once the file will be saved, it will work.
As you can see in the code, the post, page, album, movie, quote, and attachment types will be displayed. Modify that line to fit your own needs.
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 note that custom post types are not available by default on WordPress 2.9. You could have a look there if you're looking to implement that functionnality right now.
Credits goes to Justin Tadlock for this handy recipe!
By the way, if you're looking to advertise on WpRecipes, I got a free spot so be quick! Click here to buy.
3 Responses
Please update your code, in my web brokes de navigation menu,
this the original Justin code that works correctly
add_filter( ‘pre_get_posts’, ‘my_get_posts’ );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘album’, ‘movie’, ‘quote’, ‘attachment’ ) );
return $query;
}
Hi!
That’s good stuff. I did a page template instead of using a filter and, frankly, your solution uses much less code!
BTW, are you “catswhocode” (Jean Baptiste Jung) on Twitter? That cat in your header looks familiar.
-Nitin
@GonZoo thank you! I was having problems with my custom menus for the past three days thinking it was servers problems and reinstalled wordpress a few times! Please update the code so no one else has this problem.
Trackbacks: