How can I include custom post types on a tag page?

I’m using custom post types, but they don’t appear on tag pages along with regular posts and i’d like to include both standard and custom post types on tag or category pages.

What would be required before the loop to include all posts? (including custom type).

Updated:
To add, I found this code here, which worked perfectly for the first part:

Read More
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('post','cpt'); // replace cpt to your custom post type
    $query->set('post_type',$post_type);
    return $query;
    }
}

Second question still remains though:

Is there a way to allow readers to filter by post type?

So either of the following would work..

  • example.com/tag/blue?post_type=custom
  • example.com/tag/blue?post_type=post

Related posts

Leave a Reply

2 comments

  1. just to answer the second part, the syntax is:

    example.com/tag/blue?post_type[]=custom
    example.com/tag/blue?post_type[]=post
    

    or for multiple types:

    example.com/tag/blue?post_type[]=custom&post_type[]=post