Filter Brands page loop with tax_query - by saimana

This topic has 2 replies, 2 voices, and was last updated 2 years, 10 months ago ago by Olga Barlow

  • Avatar: saimana
    April 15, 2021 at 21:39

    I’m trying to filter products shown on brands page /brand/selected_brand_name/ with $_GET parameter. I when is hooked pre_get_posts on WooCommerce pages /shop archive, product category/ works fine and hooks executes. But in brands pages doesn’t hook at all.

    The shop has 2 main categories which should I use to filter brands:
    – TV Remote Controls / $_GET[‘tv’] / brand/selected_brand_name/?tv=1
    – AC Remote Controls / $_GET[‘ac’] / brand/selected_brand_name/?ac=1

    What is the user journey:
    1. User lands on home page or 2 main pages:
    – For TV Remotes:
    – For AC Remosts:
    2. User choose a brand from the list and being forwarded to:
    – For TV
    – For AC
    3. User see a list of filtered products by product category from the selected brand. Filtration is made by the $_GET parameter in the URL.

    The URL parameter I could prepend to the url of each brand in the brand list with JavaScript,
    but could you suggest me where to hook in your brands to be able to filter the products?


    Here is my code from child theme functions.php:

    add_action( 'pre_get_posts', 'smpl_modify_query' );
    function smpl_modify_query($query) {
        //if(!is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product') {
        if($query->is_main_query() && $query->query_vars['post_type'] == 'product') {
            if( isset( $_GET['ac'] ) ) {
                $query->set( 'tax_query', array (
                    'relation' => 'AND',
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => 170,
                        'operator' => 'IN',
            if( isset( $_GET['tv'] )) {
                $query->set( 'tax_query', array (
                    'relation' => 'AND',
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => 171,
                        'operator' => 'IN',
    1 Answer
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    April 16, 2021 at 13:49


    1) There are warnings on the page Maybe vecause of your custom or third-party plugins.
    2) Try to replace your line of code with following code
    4) Try to use the loop_shop_post_in filter

    add_filter( 'loop_shop_post_in', array( $this, 'show_on_sale_products' ) );
    // define the loop_shop_post_in callback 
    function custom_loop_shop_post_in( $query ){
    	//custom code here
         return $query;
    //add the action 
    add_filter('loop_shop_post_in', 'custom_loop_shop_post_in', 10, 1);


  • Viewing 2 results - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.