I want to get minimum price and maximum price based on categories.
The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category.
for example: category=”music,clothing”.
Here is query for minimum and maximum price:
$min = floor( $wpdb->get_var(
$wpdb->prepare('
SELECT min(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
AND meta_value != ""
', $wpdb->posts, $wpdb->postmeta )
) );
$max = ceil( $wpdb->get_var(
$wpdb->prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
', $wpdb->posts, $wpdb->postmeta, '_price' )
) );
Please suggest me how can i get it according to category selected.
SQL Query:
WP_Query :
This is the way how I do it :
I used
'order'=>'DESC'
so that it will sort by Highest to Lower and hence we can get Highest from it.If you want to have Min, change
'order'=>'DESC'
to'order'=>'ASC'
and you will haveecho "Min :" get_post_meta($loop->posts[0]->ID, '_price', true);
Rohil_PHPBeginner’s answer is correct, but you do no need to get prices for all products to find the maximum. You can just alter the “posts_per_page” to be 1: