DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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.

Tags

7 Comments

  1. 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;
    }

  2. 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

  3. @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.

  4. @GonZoo thanks for the update!

    I was trying to display my custom post types with regular posts in the blog roll, but couldn’t get my pagination to work. Every other page would only display the first page instead. Once I dropped in my_get_posts() in the functions.php file it worked perfect!

  5. For me, using this makes Galleries don’t show up on index page (both for normal/standard posts and custom post type). Any solution? Thanks’

  6. @Mario How about you try to give the parameter in the get_posts(post_type => ‘YourCustomPostName’); ?? they will return your Custom Posts….That works for me as well without removing the my_get_posts

  7. when I was using the above code it only added the category separately to posts and custom post type.
    But if you clicked on the category it excluded the custom post type.

    however using this code worked into bringing all of them listed under the category

    function add_custom_types_to_tax( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {

    // Get all your post types
    $post_types = array( ‘post’, ‘pfund_campaign’ );

    $query->set( ‘post_type’, $post_types );
    return $query;
    }
    }
    add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );

    but it still doesnt show them on the front page.

    As you can see on my page, only all old posts are visible the CPT Test is not visible though it shows on the backend .

    what can solve my issue?

Leave a Comment

Your email address will not be published. Required fields are marked *