
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.
17 Responses
I’m just amazed on how you discover those kinds of things. I prefer designing
Props to you. Code really does seem to be poetry
So if I wanted to show a particular page to display on the homepage? What would the code example be?
Ie: post id 89
thank you.
can’t wait for 3.0 !
You can also use with contact being the post type if you only want to display that type in a template file.
Forgot about the filter…
query_posts(‘post_type=contact’); if (have_posts()):
So you cannot do this with the current wp version?
Am keen to get a page, rather than post appearing on the home page.
Thank you.
wonderful it’s work on my wp 2.92 fr
thx you can see on my blog posh page on highlight etc
Still confuse…
Ok, let me do with this tricks on WP 3.0, later
Woohoo,
Poor me with WP 2.9.1!
Can not be patient to wait for WP 3.0
What should I do to show blog custom post types from users on my network (MU)?
WordPress 3.0 is gonna rock, with all the additions the developers are keeping for us. So far to do what’s in the code you needed to do that with categories, so i guess it’s a great addition to make stuff more logical.
This seems to knock out the new menu system on WP 3.0, at least on the index page. Menu items don’t seem to be a custom post type. No big deal, can always go back to the old fashioned way of nav menus, but I’d like to use the new 3.0 menu functionality. Anyone have a fix for this yet?
Hi,
I need to list all my custom types in a generic page, not in the home page.
I tried to code the file page.php but doesn’t work.
Anyone can help me? With a code example, please.
Bye, thanks.
Any idea how to add custom post types to archive pages?
how can I list posts from one post-type per taxonomy, something like:
Cat 01
Post 01
Post 02
Cat 02
Post 01
Post 02
…
wonderful solution. thanks for tip.
Trackbacks: