Post Pic

How to display custom post types on your WordPress blog homepage

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.

14 Responses

Feb 17 2010 10:24

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

Feb 17 2010 10:44

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.

Feb 17 2010 16:38

can’t wait for 3.0 !

Feb 17 2010 22:22

You can also use with contact being the post type if you only want to display that type in a template file.

Feb 17 2010 22:23

Forgot about the filter… :)

query_posts(‘post_type=contact’); if (have_posts()):

Feb 18 2010 10:01

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.

Mar 27 2010 07:35

wonderful it’s work on my wp 2.92 fr

thx you can see on my blog posh page on highlight etc

Apr 14 2010 14:36

Still confuse… :lol:
Ok, let me do with this tricks on WP 3.0, later

Apr 17 2010 11:24

Woohoo,

Poor me with WP 2.9.1!
Can not be patient to wait for WP 3.0 :-)

Apr 21 2010 04:02

What should I do to show blog custom post types from users on my network (MU)?

May 16 2010 01:42

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.

Jun 22 2010 22:50

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?

Jul 06 2010 17:07

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.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required