1

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?

  • What do you mean by “shuffle” exactly? – Krzysiek Dróżdż Jan 31 at 21:39
  • @KrzysiekDróżdż Sorry for my English, I need a random result, but among the 12 cars of higher price – Gabriel Henrique Jan 31 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ż Jan 31 at 21:42
  • @KrzysiekDróżdż Exact! : D – Gabriel Henrique Jan 31 at 21:43
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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