How to Add Custom Post Types to Your Home Page and Feeds

How to Add Custom Post Types to Your Home Page and Feeds

Custom Post Types in WordPress Loop and FeedCustom post types are a powerful way to extend the functionality of your website, creating a more personal experience for the viewer, and also the admin. But when you create a new custom post type, it is not automatically added to the site’s main loop. If that custom post type is one you plan to feature on the homepage of you’ll need to modify your theme’s functions.php file.

Luckily, that’s done with just a few lines of code.

Adding Custom Post Type to your Home Page in WordPress

function custom_conference_in_home_loop( $query ) {
 if ( is_home() && $query->is_main_query() )
 $query->set( 'post_type', array( 'post', 'your_custom_post_type_here') );
 return $query;
 add_filter( 'pre_get_posts', 'custom_conference_in_home_loop' );

Let’s break that down.

The first line creates a function and names it ‘custom_conference_in_home_loop, passing it a variable $query, which will be modified during the function.

The second line tests if we’re on the home page and if the query is the main query, and if those conditions are true, it executes line 3; setting the post type contained in the main query to an array containing the standard post type ‘post’, and your custom post type. The query is returned in line 4, the function closed in line 5, and finally, in line 6 we hook into the main query with ‘pre_get_posts’, which runs before template and theme functions are called. This is preferable to creating a new query or simply querying posts in a custom function, and helps speed up page load time, especially on sites with larger databases.

Basically, this function is telling WordPress before the main query is built to include our new custom post type in the home page loop, which, in-turn, adds it to the main feed, which can be accessed easily via:

Line 2 is very important – checking if you are working with the main query. If that is left out, the hook will affect other items like sidebars, menus, even admin functions. You can also see the ‘Display Posts Shortcode’ plugin by Bill Erickson for a way to do this on posts and pages without using hard code.


Separate Feeds for Custom Post Types

But what if you don’t want to give your readers the ability to grab a feed just for that custom post type, and not necessarily all post types that you might have included with the above, or similar filter.
Continuing with the event conference example above, our readers want to grab a year-specific feed and get automatic updates pertaining only to that specific year.

This presents an easy solution because it doesn’t require any code at all, just a link which takes the following form:[]=yourposttype1

or to create a feed with multiple post types use:[]=yourposttype1&post_type[]=yourposttype2

Filter the Custom Post Type Feed by Custom Taxonomy

If you created a custom post type chances are you’ve created custom taxonomies to go with it. If you want to give readers an even more granular selection of feeds, you can filter that custom post type feed further by drilling down into specific taxonomies within that post type.

Again, we continue with our conference example and now assume we offer the following hierarchical taxonomies:

  • Conference 2012 – Custom posts relating only to the 2012 conference
  • Conference 2011 – Custom posts relating only to the 2011 conference
  • Conference 2010 – You get the idea by now…

You could offer a feed showing only the current year’s conference updates by adding the taxonomy name at the end of the feed url:[]=yourposttype&yourposttype_categoryslug=actual_category

Our custom post type ‘conference-updates’ has a custom taxonomy with a slug of  “conference-year” which was set when we registered the custom taxonomy. If this were a real site, we would use something like this for the actual feed:[]=conference-updates&conference-year=2012

That link can be displayed with a custom box on your site, in a widget, as a text link, or wherever you can paste html. For more information on editing custom post types and creating custom feeds, checkout these two resources: