title: Woocommerce product filtering wp query
body: <p>A client wants only variable products to be shown as single products on category pages, and we need filter for that, i created wp-query to get those products on category pages</p>
  
  <p><strong><em>Custom query example for category page:</em></strong></p>
  
  <pre><code>&lt;?php //showing products by category
                      $cat_name = 'sommerbettdecke'; // Product category name
                      $args =  array(
                          'post_type'       =&gt; 'product_variation',
                          'post_status'     =&gt; 'publish',
                              'order'                     =&gt; 'ASC',
                          'posts_per_page'  =&gt; -1,
                          'post_parent__in' =&gt; get_variation_parent_ids_from_term( $cat_name, 'product_cat', 'name' ), // Variations
                          'tax_query'      =&gt; array( array(
                              'taxonomy'        =&gt; 'pa_jahreszeit',
                              'field'           =&gt; 'slug',
                              'terms'           =&gt; 'sommer',
                              'operator'        =&gt; 'IN',
                          ) )
                      );
                      $wc_query = new WP_Query($args);?&gt;
  
                      &lt;p class="product-counter"&gt;Products on page &lt;strong&gt;&lt;?php echo $wc_query-&gt;post_count?&gt;&lt;/strong&gt;&lt;/p&gt;
  
                      &lt;ul class="products"&gt;
                      &lt;!-- count all products on page --&gt;
                      &lt;?php if ($wc_query-&gt;have_posts()) :
                        while ($wc_query-&gt;have_posts()) :
  
                        $wc_query-&gt;the_post(); ?&gt;
  
                        &lt;!-- get attributes --&gt;
                        &lt;?php global $product; ?&gt;
                        &lt;?php $pa_fullung = array_shift( wc_get_product_terms( $product-&gt;id, 'pa_fullung', array( 'fields' =&gt; 'names' ) ) ); ?&gt;
                        &lt;?php $pa_grose = array_shift( wc_get_product_terms( $product-&gt;id, 'pa_grose', array( 'fields' =&gt; 'names' ) ) ); ?&gt;
  
                        &lt;!-- get product image --&gt;
                        &lt;?php $attachment_ids[0] = get_post_thumbnail_id( $product-&gt;id ); ?&gt;
                        &lt;?php $attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?&gt;
  
                        &lt;!-- product display start here --&gt;
                        &lt;li class="product"&gt;
                          &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;
  
                            &lt;?php if ( has_post_thumbnail( $product-&gt;id ) ) { ?&gt;
                                &lt;img src="&lt;?php echo $attachment[0] ; ?&gt;" class="card-image"  /&gt;
                            &lt;?php } ?&gt;
  
                            &lt;div class="produt-cat-loop"&gt;
                              &lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;
                              &lt;table&gt;
                                  &lt;tbody&gt;&lt;tr&gt;
                                    &lt;td&gt;Füllung:&lt;/td&gt;
                                    &lt;td&gt;&lt;?php echo $pa_fullung ?&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                    &lt;td&gt;Füllgewicht:&lt;/td&gt;
                                    &lt;td&gt;&lt;?php echo $pa_grose ?&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                    &lt;td&gt;Hülle:&lt;/td&gt;
                                    &lt;td&gt;Definisati atribut&lt;/td&gt;
                                  &lt;/tr&gt;
                                &lt;/tbody&gt;
                              &lt;/table&gt;
                            &lt;/div&gt;
  
                            &lt;button class="btn-orange"&gt;Zum Produkt&lt;/button&gt;
                          &lt;/a&gt;
                        &lt;/li&gt;
                        &lt;!-- product display ends here --&gt;
                      &lt;?php endwhile; ?&gt;
                      &lt;?php wp_reset_postdata(); ?&gt;
                      &lt;?php else:  ?&gt;
                        &lt;!-- no products message --&gt;
                        &lt;p&gt;&lt;?php _e( 'No Products' ); ?&gt;&lt;/p&gt;
                      &lt;?php endif; ?&gt;
                      &lt;/ul&gt;
  </code></pre>
  
  <p>I had to list them by category and by the attribute to get it right way. Now I have to get a sidebar filter for those products, default woocommerce product filter doesn't work with custom queries. Is there any plugin that could do the work with custom query, or is there any way to get variable products as single products and to get products on category pages by category and attribute in default woocommerce loop?</p>
  
  <ul>
  <li><em>when listing products on category page i have to list them not only by the category slug but also by the attribute(pa_jahreszeit) value, in this query example that attribute is 'pa_jahreszeit' and its value is 'sommer' - not sure if this can be done by default woocommerce loop from archive-product.php</em></li>
  </ul>
  
  <p>Update:
  -in sidebar I need to filter products by attribtes and price</p>
  
  <p>Thank you in advance, sorry for misspelling </p>
  
  <p><strong>Update vol.2:</strong>
  Here is a code from functions.php - using pre_get_posts</p>
  
  <pre><code>add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
  function rc_modify_query_get_design_projects( $product ) {
    $product-&gt;set('post_type', 'product_variation');
    if (is_category( '2312' ) ) {
      $taxquery = array(
          array(
            'taxonomy'        =&gt; 'pa_jahreszeit',
            'field'           =&gt; 'slug',
            'terms'           =&gt; 'sommer',
            'operator'        =&gt; 'IN'
          )
      );
      $product-&gt;set( 'tax_query', $taxquery );
    }
  }
  </code></pre>
  
  <p>When i put taxquery outside of <code>if (is_category( '2312' ) ) {</code> it works fine, but while the taxquery is inside this condition it doesnt work, it shows all variable products :/</p>