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;
- }
// 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;
- }
// 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!
return $qv;







Recent Opinions