Restrict post types in taxonomy archive

Author: Aleš Sýkora, 29. 10. 2019

If you need to restrict which custom post types will display in taxonomy archive, you need to do it with some PHP. Here is how to do it.

For example: You are using same taxonomy called "Product category" for 3 Custom Post Types. Two of them are products - Cups, Plates. 3rd one is helper for Call To Actions displayed for each category. So you do not want to display CTA in Product Category Archive. Then you need to use this code snippet and define which CPTs should be displayed.

Create a code snippet at Toolset > Settings > Custom Code.

 * Restrict post types included in taxonomy archive
 function ts_modify_tax_archive( $query ){
    $tax = 'category-slug'; // Edit taxonomy slug
    if ( $query->is_tax( $tax ) && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'slug1', 'slug2' ) ); // Edit slugs of post types to include
add_action( 'pre_get_posts', 'ts_modify_tax_archive' );