Aleš Sýkora / October 29, 2019 / 0 comments
Restrict post types in taxonomy archive
min read / WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: 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.
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' );
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻