Skip to main content
removed else statement to preserve default behavior
Source Link
locomo
  • 1k
  • 7
  • 6

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

yourwebsite.com/topics/cancer/

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

yourwebsite.com/topics/cancer/?post_type=question

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'] );
        } else {
            // show all results
            $query->set( 'post_type', $post_types );
        }
    }
}

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

yourwebsite.com/topics/cancer/

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

yourwebsite.com/topics/cancer/?post_type=question

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'] );
        } else {
            // show all results
            $query->set( 'post_type', $post_types );
        }
    }
}

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

yourwebsite.com/topics/cancer/

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

yourwebsite.com/topics/cancer/?post_type=question

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'] );
        }

    }
}
Source Link
locomo
  • 1k
  • 7
  • 6

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

yourwebsite.com/topics/cancer/

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

yourwebsite.com/topics/cancer/?post_type=question

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'] );
        } else {
            // show all results
            $query->set( 'post_type', $post_types );
        }
    }
}