$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Random order of WP_Query results with highest meta value|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Random order of WP_Query results with highest meta value

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far