php - Adding URL Query parameters to WP_Query -


i implementing product filter woocommerce store. want filter product base on attributes, such color, can retrieve url query parameters. example, if path /product-category/clothing/?filter_color=16, product color id = 16 shown.

right feature seems available when added widget yith woocommerce ajax product filter plugin. however, not want use plugin because not consistent other features , implement own. couldn't find how yith achieve this.

i want make work both main loop , custom loops. main loop referring this:

<?php while ( have_posts() ) : the_post(); ?>     <?php wc_get_template_part( 'content', 'product' ); ?> <?php endwhile; // end of loop. ?> 

and custom loops:

                $args = array(                     'post_type' => 'product',                     'posts_per_page' => 12,                     'product_cat' => $category->slug,                     'orderby' => 'menu_order',                     'order' => 'asc'                     );                 $loop = new wp_query( $args );                 if ( $loop->have_posts() ) { ... 

you can check query, retrieve value of color, check if match , if display product. have at:

<?php while ( have_posts() ) : the_post(); ?>     <?php      if(isset($_get['filter_color']) //check if filter color set     {         $color=$_get['filter_color'];          $productcolor = get_the_terms($product->id,'pa_color');          if ($color == $productcolor) //if filter color matches color of prodct             wc_get_template_part( 'content', 'product' ); //then show product     }      ?> <?php endwhile; // end of loop. ?> 

same approach applies custom loops.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -