Aleš Sýkora wrote

Restrict post types in taxonomy archive

29. 10. 2019 in

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Automatic Post to Facebook From WordPress with Custom Fields
7. 3. 2019 in
If you want to post on your facebook page automatically when you publish the post & you want to use custom post types and custom fields made in Toolset Types - follow this post and see how easy it is.
Read More
Aleš Sýkora wrote
Child posts menu for custom post type parent item
1. 2. 2021 in
This code will help you display menu of childs on current post. For example: Product 1 = parent has those child pages: – Product usage– How to– Disclaimer– Calculator When you put this child items menu in your template, it will show all childs of product 1 on all child pages and also on product […]
Read More
Aleš Sýkora wrote
How to ignore sticky posts in Oxygen repeater?
6. 9. 2021 in
If you create WordPress query with Oxygen Builder’s repeater element, then you propably want to ignore sticky posts sometimes. This is how you can do that with advanced query builder. You have two options of ignoring sticky posts: Ignore them from display pinned on the top, but display them in results of query Do not […]
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram