php - Random order of WP_Query results with highest meta value

admin2025-06-03  3

I need to random of cars with highest price.

My code:

$argsLoop = array(
    'post_type'      => 'cars',
    'posts_per_page' => 12,
    'paged'          => 1,
    'meta_key' => 'cars_price', 
    'orderby' => 'cars_price',
    'order' => 'DESC'
);

How to leave the result of this query random?

I need to random of cars with highest price.

My code:

$argsLoop = array(
    'post_type'      => 'cars',
    'posts_per_page' => 12,
    'paged'          => 1,
    'meta_key' => 'cars_price', 
    'orderby' => 'cars_price',
    'order' => 'DESC'
);

How to leave the result of this query random?

Share Improve this question edited Jan 31, 2019 at 21:50 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 31, 2019 at 21:37 Gabriel HenriqueGabriel Henrique 509 bronze badges 4
  • What do you mean by “shuffle” exactly? – Krzysiek Dróżdż Commented Jan 31, 2019 at 21:39
  • @KrzysiekDróżdż Sorry for my English, I need a random result, but among the 12 cars of higher price – Gabriel Henrique Commented Jan 31, 2019 at 21:40
  • No reason to be sorry about. I just wanted to be sure that we understand it the same way... :) So you want the 12 card with lowest prices and then shuffle these cars so they are showed in random order, right? – Krzysiek Dróżdż Commented Jan 31, 2019 at 21:42
  • @KrzysiekDróżdż Exact! : D – Gabriel Henrique Commented Jan 31, 2019 at 21:43
Add a comment  | 

1 Answer 1

Reset to default 1

OK, so you're almost there. Let's say you have this query:

$argsLoop = array(
    'post_type'      => 'cars',
    'posts_per_page' => 12,
    'paged'          => 1,
    'meta_key' => 'cars_price', 
    'orderby' => 'cars_price',
    'order' => 'DESC'
);
$cars = new WP_Query( $argsLoop );
// All you have to add is this line:
shuffle( $cars->posts );

Now you can do standard loop and the selected cars will be showed in random order.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748956093a315137.html

最新回复(0)