0

$max_query = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'orderby'=>'meta_value_num', 'order'=>'DESC','meta_key'=>'_price','posts_per_page'=>1) );

if(have_posts()){

while ($max_query->have_posts()){

$max_query->the_post();

$maxp = get_post_meta( get_the_ID(), '_price', true );

}

I am using this and its worked perfectly but how to retrieve max or min price woocommerce without using wp query? actually I am using it for price range slider(dynamically).

3
  • You want to retrieve products with prices between the given max and min price? Like a filter? You will need meta_query for that.
    – Abhik
    Commented Apr 7, 2022 at 13:41
  • Have you thought about using the REST API instead woocommerce.github.io/woocommerce-rest-api-docs/… Commented Apr 7, 2022 at 14:10
  • @Abhik No, first I want to bring the highest and lowest price of woocommerce products.Ex: product price start from 10 and highest is 150.so I just want only 10 and 150 first.
    – Kou WP
    Commented Apr 7, 2022 at 14:25

1 Answer 1

1

This should do the job.

$products = get_posts(array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'orderby' => 'meta_value_num',
    'meta_key'=> '_price',
    'posts_per_page' => -1,
));

$highest = $products[array_key_first($products)];
$lowest = $products[array_key_last($products)];

$highest_price = get_post_meta( $highest->ID, '_price', true );
$lowest_price = get_post_meta( $lowest->ID, '_price', true );
1
  • Thanks @Abhik. it work like a charm. Thanks a lot man....:)
    – Kou WP
    Commented Apr 11, 2022 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.