Back to Top

Include Custom Post Types in Default Post Listings

Previous Post:

Include Custom Post Types in Default Post Listings

WordPress itself comes with many useful built-in pages such as archive pages and feed pages, but those pages do not include custom post types by default. This is actually an expected hehaviour, because most people want custom post types to have their own archive and feed pages.

If you are the odd one out and would like to put custom post types into normal post listings instead, one simple filter will be everything you need ;). Fire up your theme’s functions.php and add the following (position doesn’t matter):

// Include Custom Post Types in Default Post Listings
// betterwp.net/wordpress-tips/include-custom-post-types-in-default-post-listings/
add_filter('request', 'bwp_request');
function bwp_request($qv)
{
	if (isset($qv['pagename']) || isset($qv['name']) || isset($qv['p']) || isset($qv['page']))
		return $qv;
	if (isset($qv['tag']) || isset($qv['year']) || isset($qv['month']) || isset($qv['day']) || isset($qv['feed']))
	{
		$qv['post_type'] = array('post');
		if (!isset($qv['feed']) || empty($qv['withcomments']))
		{
			array_push($qv['post_type'], 'post_type_a');
			array_push($qv['post_type'], 'post_type_b');
			// ... repeat this until you are satisfied
		}
	}
	return $qv;
}

Yes, that’s all you have to do, did you expect more ;)?

If you don’t like having to manually input those post types, simply use get_post_types()1. And if you just want to include custom post types in your post feed (i.e. the global feed that show all recent posts), use this snippet instead:

// Include Custom Post Types in global feed
// betterwp.net/wordpress-tips/include-custom-post-types-in-default-post-listings/
add_filter('request', 'bwp_request');
function bwp_request($qv)
{
	if (isset($qv['pagename']) || isset($qv['name']) || isset($qv['p']) || isset($qv['page']))
		return $qv;
	if (isset($qv['tag']) || isset($qv['year']) || isset($qv['month']) || isset($qv['day']) || isset($qv['s']))
		return $qv;
	else if (isset($qv['feed']) && empty($qv['withcomments']))
	{
		$qv['post_type'] = array('post');
		array_push($qv['post_type'], 'post_type_a');
		array_push($qv['post_type'], 'post_type_b');
		// ... repeat this until you are satisfied
	}
	return $qv;
}

Refresh the pages and enjoy!

if (isset($qv[‘pagename’]) || isset($qv[‘name’]) || isset($qv[‘p’]) || isset($qv[‘page’]))
return $qv;

References

  1. http://codex.wordpress.org/Function_Reference/get_post_ ... post_types []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: