I have a number of custom post types as well as a custom taxonomy. I created an archive page for the taxonomy which lists each connected post, so all good there. What I would like to achieve is the following...

On my single template for that taxonomy (I've called it Topics), say if the topic was 'cancer', I would like a dropdown select menu or list of links to each custom post type WITH that taxonomy label. In other words, if I'm showing all the posts tagged with 'cancer' for every post type (which I currently have), that dropdown or link list at the top of the page would allow me to only show the tagged posts for each post type, as in the following:

Showing posts with topic: cancer

Select a post type to view posts with this topic:

Post type name 1 (link)
Post type name 2 (link)
Post type name 3 (link)

(all posts tagged with cancer already display here...)

Post type 1, when clicked, loads the single taxonomy page again but only lists the posts tagged with 'cancer' for that post type. This would apply to any of the few dozen tags for that custom taxonomy, so I can't hard-code the URL. It needs to be dynamic.

I hope this makes sense. I've searched for two days on this and so far have come up with nothing. Any help or direction would be appreciated. Unfortunately, the project I'm working on is gated and I can't share any links. I'm also using the Custom Post Type UI plugin (not by choice) and I know that plugin doesn't work well with taxonomies but I'm hoping my hands aren't tied here.


2 Answers 2


If the URL to the 'cancer' taxonomy term in your example looked like this:


then you could filter these results by post type with a URLs structured like this:


Just put this in functions.php

add_filter( 'pre_get_posts', 'wp123_post_type_by_taxonomy' );
function wp123_post_type_by_taxonomy( $query ) {
    if( is_tax( 'topics' ) && $query->is_main_query() ) {

        // get all post types:
        $post_types = get_post_types();

        // or add specific post types:
        // $post_types = array( 'post_type_1', 'post_type_2' );

        if ( !empty( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) {
            // show only results for this post type
            $query->set( 'post_type', $_GET['post_type'] );

  • You should remove the else statement to maintain default behaviour and avoid overwriting the post_type parameter for all the other cases that we don't care about.
    – Anastis
    Oct 6, 2018 at 18:29
  • good point, i've removed the else statement to preserve default behavior
    – locomo
    Oct 20, 2018 at 13:42

According to what I understand is :

In your taxonomy.php

Add this code :

 $term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
 echo '<h2>Showing posts with topic : ' . $term->name .'</h2>';

This will result in showing "Showing post with topic : Cancer"

Now for (all posts tagged with cancer already display here...)

You can run a get_posts() to get this :

$all_post = get_posts(array(
    'post_type' => array( 'cpt1', 'cpt2', 'cpt3', 'post' ), // all cpt slug name
    'numberposts' => -1, // number of post
    'tax_query' => array(
             'taxonomy' => get_query_var( 'taxonomy '), // current tax name
             'field' => 'id',
             'terms' => $term->term_id, // current tax_id

By looping over them we can get all post details and we can display them.

  • 'field' => 'id' is incorrect, it should be term_id. Feb 2, 2016 at 9:17
  • Also, the term object is already available, why make yet another db call to get the current term object Feb 2, 2016 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.