Articles in this series

80 responses to “Showing custom post types on your home/blog page”

  1. Cristian Antohe

    One thing I don’t really understand is how/where I select what post type I’m about to write in the backend. Particularly when we don’t have a UI for that yet.

    1. Matt Wiebe

      I believe that the UI stuff is what’s coming in 3.0.

    2. Patrick Daly

      If you’re using the trunk version you can see the UI, but you have to register your post type first.

      function post_type_movies() {
      	register_post_type('movies', array('label' => __('Movies'), 'public' => true ) );
      	register_taxonomy_for_object_type('post_tag', 'movies');
      }
      
      add_action('init', 'post_type_movies');

      Just replace “movies” with whatever you want.

      1. Cristian Antohe

        Ok. This makes sense. So there is no real way of using custom post types with 2.9.1 :)

      2. Dustin Dempsey

        Nice, can’t wait for your full tutorial when 3.0 is released.

        I feel these will evolve into complete custom write panels, image the possibilities.

      3. Jason Pelker

        It’s difficult to be patient for all the features coming in 3.0! It’ll likely be the most revolutionary WordPress version yet and in all truth, it will subsequently become the defacto CMS throughout the world.

      4. Elio

        As Justin says, in addition to the code you still have to do the UI panel. Right now WP 3.0 trunk doesn’t display any UI even after registering post types.

      5. David Tufts

        I got this to work in the current trunk including the UI panel with a couple of simple additions to you code.

        function post_type_movies()
        {
        register_post_type('movies', array('label'=>__('Movies'), 'public'=>true, 'show_ui'=>true, 'exclude_from_search'=>false));
        register_taxonomy_for_object_type('post_tag', 'movies');
        }

        add_action('init', 'post_type_movies');

  2. Abu Aufa's Weblog

    Wow.. great features, Can’t wait for WP 3.0

  3. Michael Fields

    Great example! I’ve never used $query->set() before. I believe that you just opened up a whole new world for me. Thanks!

  4. Peter

    Very useful information. Especially for a newbie like me. I will try to implement that on my blog. Thanks.

  5. Andrew

    Wordpress 3.0 is coming? I don’t think I’ll upgrade that quickly when it comes out, maybe I’ll have to wait it out till 3.1 comes out.

  6. Nathan Rodriguez

    Cool feature, but i think is better wait for the 3.0 version of WP, im not confident enough on messing on installation files.

  7. Rinaldi Syahran

    Hi, another use from this wordpress 3.0 . Very thankful to you for sharing.

  8. Bhandari Group

    I will try this one as soon as i get WP 3.0. A great stuff to help the bloggers. Really i need the code for that and you have provided it.

  9. Karen Mae Farro

    I can’t wait for Wordpress 3.0! I’ll be waiting for your tutorial.

  10. First Impressions of Custom Post Type - label, public, excludefromsearch, publiclyqueryable, showui, inherittype - WP Engineer

    [...] Justin had been playing with these types, we check out the possibilities of types for [...]

  11. shawn

    Am I correct to assume that 3.0 will introduce new admin panels for custom post_types?

    So in 3.0 if I create a custom post type ‘movies’ I will then have a new ‘movies’ choice in the admin panel where the user could input the information relevant to ‘movies’ only?

    Right now with 2.9 I basically create new meta field boxes for the admin to fill in while writing a post. The problem comes into effect when I have ‘movies’ as a type of post, and say ‘music’ as a second type.

    Right now in 2.9 both the meta input boxes show up in the ‘add article’ admin section, which makes it really confusing to the user. They have the ability to input information both for movies and music on the same ‘add article’ admin page.

    This would indeed go a long ways into turning wp into more of a cms, where the author has a much clearer path as to where to input the data for each type of post…. boy I sure hope I am right.

  12. Jason Pelker

    How do custom posts jive with traditional taxonomies, such as categories and tags? If I queried a category, would all posts and (musics, et al) that have that category display?

    1. Matthew Simo

      I’m interested in this, I heard that they were adding the Categories and Tags taxonomies to Pages so, at least in my mind, it makes sense that they would apply to both. And I’m assuming that custom-post types would also be applicable, as long as you made sure they were registered for those taxonomies.

      Which, if that is true then it makes sense that any custom taxonomies could also be registered across multiple custom post-types, as well as your posts and pages, if you desired as well..

      Very exciting stuff.

  13. palPalani

    nice feature, this is very useful for me.

  14. L’Hebdo WordPress : WordPress 2.9 – WordPress 3.0 – BuddyPress

    [...] qui permet d’attribuer un genre à un article. Cette fonction est reprise en détail par Justin Tadlock. Ensuite, WordPress 3.0 autorisera la personnalisation simplifiée des arrières-plans de nos sites [...]

  15. WordPress Picks for the Week [02/17] | Techtites

    [...] Showing custom post types on your home/blog page [...]

  16. How to display custom post types on your WordPress blog homepage

    [...] goes to Justin Tadlock for this handy [...]

  17. Elad

    Hi,

    As you wrote, if I remove the ‘attachment’ type I’ll get the posts without the images. I would like to get the images without the posts but when I omit the ‘post’ type’, and leave onle the ‘attachment’ type, I get nothing.

    Any idea?

  18. Robert

    I’ve just been playing around with this in WP 3.0 svn and so far so good. I can easily create a new post type, assign the default post features and custom taxonomies and get it all in a custom write panel. It’s also easy to create custom meta boxes linked to the new post type.

    What I’m wondering about is:

    1. How will adding custom fields to the custom post type be managed (one could just use the post custom fields but what if it’s a more complex post type that warrants its own DB table.)
    2. I’d like to see an interface to also be able to add arbitrary custom fields to the Quick Edit part on the post list.

    I guess what I want to be able to do is create custom front end functionality depending on the post type. For example:

    Event Post Type
    - display additional fields like event date, duration, venue, contact, etc…
    - change the post sorting to use event start date or venue or any other additional field.

    So basically my front end template will have different functionality than the default post type of ‘post’…

    I guess I’m jumping ahead a bit here but just wanted to voice my thoughts :) .

  19. MostlyBlog

    wow great post thanks for sharing with us

  20. vpsbul

    nice feature!

  21. The Real David Tufts » Blog Archive » Using Custom Post Types in Wordpress 3.0

    [...] make sure to check out what Justin Tadlock has to say on this [...]

  22. Nina

    Totally helpful. Thanks a lot :)

  23. life is real

    I’ve problem..How do custom posts such as categories and tags?

  24. WordPress 3.0-alpha – pasul către un CMS adevărat? | WP Tuts

    [...] Showing custom post types on your home/blog page [...]

  25. Iva

    Awesome tutorial, but what would I have to do if I wanted to query only one specific type in the sidebar? I tried your Query Posts widget to query all the items from a non-hierarchical category “song” and it’s impossible.

    And how can I assure that a custom post type will be page-like? I was trying to get rid of some things in the display on “song” post type and make it output values of certain custom fields by making it a custom template and I realised that it’s not possible, because it’s post-like, not page-like. Would creating a template file song.php result in anything by default? I thought this is what add_post_type_support() could do but I have not seen any references to that yet.

    I guess that I can’t wait for the rest of this series, as so many things are left unsaid and your and WP Engineer’s posts on this both left everyone curious. :)

  26. Bill Perciballi

    We are going to kick-off a blog for our company. And, I’m curious as to why you chose WordPress over say TypePad or Blogger. What’s the advantage?

  27. Best WordPress Development Tutorials From February 2010

    [...] Showing custom post types on your home/blog page [...]

  28. Mediebureau

    does this work with xampp? because i am using one i don’t if it will work.

  29. Mike Schinkel

    Justin,

    Nice post, thanks.

    One thing though is that you example as written whacks out the new menu system since menu items are not custom post types. You should change it (I think) to

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    
    	if ( is_home() && $query->is_posts_page )
    		$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'attachment' ) );
    
    	return $query;
    }

    That works for me but there may be an even more robust way to handle it.

    1. Rich

      Thanks Mike. My menu vanished after a very long session and this was a life saver!
      This solves it 100%

      1. Rich

        Actually this has now conflicted with something else. Is there a definative way of achieving this in WordPress 3.0 without loosing your custom menu?

      2. ivan.chaquea

        Triying to achive this result, not on home but categories. im updating the code as i find out more, but still not working properly

        now the wp_nav_menu only conflicts on the category page, everything else is showing normal

        http://wordpress.org/support/topic/418330

  30. Marc Deschamps

    Most usefull post, but still can’t wait for wp3.0 :-)
    I also agree with Jason Pelker, WP will become defacto CMS and still be easy to host it anywhere.

  31. Toeic

    This is very helpful. Thank you very much. Are you going to write a bit more about it?

  32. illimar

    One thing I noticed with using the Wordpress dev version is that the edit post/page title has changed. It probaby is constructed something like this: “Edit $post_type”.

    This is bad, because it doesn’t take other more complex languages into account.
    For example, both finnish and estonian have over a dozen of different cases, and this is one of the places where they are used.
    For example, while editing a page, in estonian this would display “Muuda leht”, whereas it should be “Muuda lehte”.

  33. HeroicNate

    What does it mean when you get: “Warning: Illegal offset type in isset or empty in /home/content/html/wordpress/wp-includes/query.php on line 1715″?

  34. links for 2010-03-28 | Links | WereWP

    [...] Showing custom post types on your home/blog page WordPress 3.0 introduces custom post types, a new step to improve the CMS capabilities of WordPress. Here is a way to include them into your loop. (tags: 3.0 wordpress development) Leave a Reply Click here to cancel reply. [...]

  35. Sam DeSocio

    the thing I don’t understand is from a theme creation perspective how do we call these in a specific way. It seems like you did that in your screen shot but didn’t speak to it in the post.

  36. Trying to Add New Post Types | Go

    [...] Instructions on how to make custom post types show up is available, courtesy of Justin Tadlock (thanks to Brad Williams for sending me to Justin’s post). They involve edits to WordPress PHP files. At the time of writing, I haven’t made those edits to this install. This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post. ← Fuseki [...]

  37. Daily Digest for April 24th

    [...] Shared Showing custom post types on your home/blog page. [...]

  38. Wordpress 3.0 Insight – main features and settings

    [...] this site and this site if you want to learn more about the custom post [...]

  39. fb

    Once again another solution, to save us a lot of extra editing…

    Now if we could only sort out custom post_type archives that would be great.

    Keep up the great work

  40. Mark

    I really need to create a new set of pages right now in a 2.92 site. But if I were on 3.0 already, I’d want to make them a custom post type instead. But I’m hearing this is 2 weeks out at least.

    So…

    In 3.0, can I convert a normal Page to a Custom Post Type? Likely not in Admin, so if it’s not, would I just be able to go into the database and change the post type field to the name of the custom post type, from “Page?”

  41. Ted

    Its ok, but WP 3.0 now in beta, on front page i see only 2,9,2?

  42. Ovidiu

    nice. now how can I convince the Leviathan theme to show the byline before the post and the post-meta after the post for my custom post types just like it does for regular posts?

  43. Ovidiu

    oh, just noticed that the settings for Hybrid recipe settings, recipe being my new custom post type, are not saved when saving a new recipe. any idea what could be wrong here?

    Using wp 3.0 RC2 and your 0.8.1 beta hybrid theme + Leviathan

  44. Template Changes/Tags for Wordpress 3.0

    [...] Show Custom post Types on your Homepage by Justin Tadlock [...]

  45. Damian

    If I create a custom post, how can I filter my query to a specific category?

    Let’s say a create a custom post type called ‘Music’ and I create 3 categories : ‘jazz’, ‘classical’ and rock. How can I only display the ‘jazz’ and ‘rock’ entries??

    This’s not wok query_posts('post_type=uslugi&cat=17,19'); ?

     array('uslugi'),
        'caller_get_posts'=>1,
        'posts_per_page'=>1,
        'order' => 'ASC',
        'kat-uslugi' => 'Nasze Usługi', // how add more categories ??
        'showposts' => 4,
    );
    
    //query_posts($args);
    $my_query = new WP_Query($args);
    ?>
  46. Wordpress 3.0 Roundup | Themergency

    [...] Showing custom post types on your home/blog page - Justin Tadlock A good post that shows how to add custom post types to your home page or in your feeds [...]

  47. Getting Down and Dirty with WP_Query->set() | Michael Fields: WordPress Development

    [...] was so stoked when I was reading a blog post on Justin Tadlock’s site about custom post types. He introduced me to a new way of hooking [...]

  48. Adam

    I think this will work really good with the new wordpress 3.0. What do you think?

  49. Anna-Maria Eriksson

    Hi!
    This doesn’t seem to work anymore with the final release of 3.0. I don’t know why, but I get a syntax error (in dreamweaver cs5) when I paste this code inside.

    I would like to have my CPT’s displayed in the main loop, just like this code snippet does, but for some reason it just doesn’t work anymore…

    Any ideas on why?

  50. Around the WordPress Community: HTML Emails, CSS3, Custom Post Types, Theme Design, and Taxonomies

    [...] from Subversion every 30 minutes. This will help people search for code references and resources.Showing custom post types on your home – Justin Tadlock – Working with custom post types, check out Justin’s article on how to bring those [...]

  51. Michael

    This works great, but I’m running into a problem. Let’s say you use this to show every post type on your homepage. But in the sidebar, you want to list the last five posts of one particular post type. So you set up a get_posts loop with the post_type specified. It doesn’t work. $query->set overrides your get_posts argument and shows the last five of all post types. What would be the best way to circumvent this problem?

    1. Josh

      I had to change the post query just before the post loop. This allowed me to do exactly what you are trying to do. This also doesn’t break the WordPress menus. You can check it out at http://think2loud.com/?p=763

  52. xefned

    It worked!
    Although for some reason I had to make my post type plural in the code

    'post_type', array( 'post', 'page', 'albumS'...
  53. Useful Resources and Tutorials to get started with WordPress 3.0 | ryanriatno

    [...] Showing custom post types on your home/blog page [...]

  54. Displaying custom post types on your WordPress blog homepage – Think2Loud

    [...] ); } ?>if you want to read more about the first piece of code you can check it out on Justin Tadlock’s blog.                  Related PostsHarness the Power [...]

  55. helpin

    I entered the code in functions.php
    under function editoptions() {

    .But It doesn’t make any changes for any site in my multisite network. Am I missing anything?

    Thank you !

  56. Afficher les “Custom Post Types” sur votre blog et flux RSS ! sur Geekeries.fr | Découvrir WordPress

    [...] Justin Tadlock met avant la simplicité d'afficher les nouveaux types de modules d'articles sur la page d'accueil de votre blog WordPress, mais également dans flux d'informations. En effet, bien que cette fonctionnalité est très efficace elle reste néanmoins accessible qu'à une partie des utilisateurs du gestionnaire de contenu qu'est WordPress. Ces types de modules d'articles peuvent être créés et gérer directement depuis l'interface d'administration de votre blog via l"utilisation de l'extension "Custom Post Type UI" (déjà présenté). Il existe également une extension pour gérer, depuis votre interface de création de contenu, l'affichage de vos nouveaux modules d'articles. [...]

  57. Ami

    Just what I’ve been looking for but problem I have run in to a problem.

    All my posts show on the home page page as I wish but I also have a gallery page which I would only like to display my gallery custom post types.

    This is my code in page-gallery.php:

    If I remove Justin’s code from my function.php then the only the gallery posts will be shown on the page but no on my home page.

    Any idea’s? Or have I missed something?

    Thanks

  58. Renee

    Where would you change is_home()?

  59. Margarida

    Hi there Justin,
    I have just started a new blog, and I am using a child theme for twentyten. I created two post types and noticed that they don’t show up at all. Even with a category assigned to them, they don’t show up in category page.
    So I inserted the code you gave here in my function.php, and post types already show up in my homepage, but I get this error:

    Warning: Illegal offset type in isset or empty in /home/magawork/public_html/criaturasblog/wp-includes/post.php on line 736

    Warning: Illegal offset type in isset or empty in /home/magawork/public_html/criaturasblog/wp-includes/post.php on line 736

    I have no idea what this means.
    Can you help me please?

  60. JeremyT.

    Hi Justin, thanks for this code. I don’t pretend to fully understand it, but I will try to implement this feature. I like the flexibility of adding post type to the blog page.

  61. nickmorss

    Im getting the same issue as Ivan.

    I want to be able to show my custom Post types when i filter by category… Ivans code works but with nasty side effects

    http://wordpress.org/support/topic/adding-custom-post-type-to-the-loop-wp-nav-menu-dissapears?replies=5#post-1645931

    HELP!!!

  62. Ovidiu

    I currently use this code, as per your example to show different custom post types on my home page.

    /*show pages and recipes on home page */
    function my_get_posts( $query ) {
    
    if ( ( is_home() && false == $query->query_vars['suppress_filters'] ) || is_feed() )
    $query->set( 'post_type', array( 'post', 'page', 'recipe') );
    
    return $query;
    }
    
    ?>

    can you help me change the code to exclude posts from a certain category from being displayed?

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.