WordPress: Adding Custom Post Types to Home and RSS Feed

I’m  currently rebuilding the Invisible Agent website, something long long overdue. The current site was built about 7 years ago and is feeling its age.

Part of the process of building the site has been to completely re-arrange the handling of content types, however when using custom post types we need to update how WordPress filters the default queries for our home page, tag archives, and RSS feed if we want our custom post types to show up as desired.

A little background

The old site was built before WordPress released “Custom Post Types” and we’re gearing up to take full advantage of this functionality.

The site had custom post types for releases and podcasts, to better delineate content in the back end and make queries more flexible at the front end. Additionally we’ve got custom post types for stores, artists, and ‘call to action’ content types.

Warren wanted to have his ‘home’ view showing a reverse chronological view of not just blog posts but releases and podcasts. By default the WordPress ‘front page’ setting only shows blog posts so we need to instruct it to also include the other post types we need.

Whilst we’re at it, we want to make sure releases and podcasts are included in the RSS feed. Fortunately we can do this with one function and an add_filter action.

Add custom post type to both RSS and front page

Open up your theme’s functions.php file and paste in the code below.

add_filter( 'pre_get_posts', 'pa_add_custom_posts_to_home_and_feed' );

function pa_add_custom_posts_to_home_and_feed( $query ) {
if (
((is_home() || is_tag()) && $query->is_main_query())
|| is_feed()
):
$query->set( 'post_type', ['post', 'podcast', 'release']);
endif;
return $query;
}

Obviously you’ll need to update the post type setting on the line starting $query->set.

How does this work?

We have our add_filter call, which is just telling WordPress that during pre_get_posts, it should also run our custom function pa_add_custom_posts_to_home_and_feed.

The actual function itself just checks a series of conditionals, and if our current WP_Query matches any of the conditionals, then we update what post types are being included in WP_Query. First we check is_home() and in our case the Settings > Reading option for what the home page shows is just the default blog posts option.

Next up, because the site may be using tag archives, and tags are utilised by posts, podcasts and releases, then we also heck if the current query is_tag(). Whilst here though we also have to confirm that the $query->is_main_query(), or else we get some strange behaviour when viewing tag archives.

Finally we use the is_feed() conditional to make sure we’re covering our RSS feed.

If any of these conditions are met, then we update the current WP_Query to include our custom post types.

If the conditions aren’t met, we just return the $query untouched.

I hope you’ve found this short tutorial helpful, and if you have any questions, please don’t hesitate to let me know in the comments below.

Leave a comment