Forked from navnit-viradiya/wp-query-with-meta-query-and-taxonomy.php
Created
September 8, 2020 18:41
-
-
Save slaffko1/aae9ef4337e9d0450a2c2fbe8d091bec 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