Created
September 27, 2018 10:23
-
-
Save navnit-viradiya/07fcd47a7f8cd6604244ac3515d9a504 to your computer and use it in GitHub Desktop.
WooCommerce search products between price range using WP_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'tax_query' => array( | |
'relation' => 'AND', | |
array ( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => array('clothing'), | |
'operator' => 'IN' | |
), | |
), | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => 'color', | |
'value' => 'red', | |
'compare' => '=', | |
), | |
array( | |
'key' => '_price', | |
'value' => array(30,40), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC' | |
) | |
), | |
); | |
$the_query = new WP_Query( $args ); | |
echo $count = $the_query->post_count; | |
if ( $the_query->have_posts() ) : | |
while ( $the_query->have_posts() ) : $the_query->the_post(); | |
echo '<li>' . get_the_title() . '</li>'; | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment