• Resolved sroskylos

    (@sroskylo1)


    When showing all products from this brand in the brand page, how can exclude an category?

    I need not showing the products that belongs to a specific category.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor titodevera

    (@titodevera)

    Hi sotos.

    You can use pre_get_posts hook for modify the query and exclude the terms you want of a especific taxonomy (“product_cat” in this case).

    Add the code below to your functions.php:

    
    add_action( 'pre_get_posts', 'pwbc_exclude_product_categories' );
    
    function pwbc_exclude_product_categories( $query ) {
    
        $categories_to_exclude = array( 'albums' );//this categories will be excluded (by slug)
        $queried_object = get_queried_object();
    
        if( is_a( $queried_object, 'WP_Term' ) && $queried_object->taxonomy == 'pwb-brand' && is_archive('product') ) {
    
          //is brand page
    
          $tax_query = array(
              array(
                  'taxonomy' => 'product_cat',
                  'field'    => 'slug',
                  'terms'    => $categories_to_exclude,
                  'operator' => 'NOT IN'
              )
          );
    
          $query->set( 'tax_query', $tax_query );
    
        }
    
    }
    

    ??

    Thread Starter sroskylos

    (@sroskylo1)

    Works like a charm!

    Thank you so much @titodevera!

    • This reply was modified 7 years, 10 months ago by sroskylos.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cateegory exclude’ is closed to new replies.