2

Attention: It's order, not orderby.

According to wordpress docs, there are only two options to order, which are ASC or DESC.

The problem is:

I would like to randomize the posts I've selected, not randomize WHICH posts I select.

Here's my code for better explanation:

<?php
return array(
  "post_type" => "listings",
  "post_status" => "publish",
  'meta_query'=>array(
                        array('key'=>'featured_until','value'=>$current_date, 'compare'=>'>'),
                     ),
  'meta_key'=>'featured_until',
  "orderby" => array(
            'featured_until' => 'RAND', /* How can I make this work? */
            'date' => 'DESC'
  ),
  "suppress_filters" => true,
  "facetwp" => true,
  "posts_per_page" => 16
);

It's a listings website. The code above selects 16 posts to show on the first page.

First, it tries to find 16 featured listings. If there aren't that many featured listings, it completes with regular listings ordered by date.

The question is: How can I order the featured listings by RANDOM, instead of ASC or DESC?

4
  • Your query is wrong. Orderby cant contain array like yours. codex.wordpress.org/Class_Reference/WP_Query $args = array( 'orderby' => 'rand', 'posts_per_page' => '1', ); $query = new WP_Query( $args );
    – Foxsk8
    Commented Sep 13, 2016 at 18:58
  • 1
    I respectfully disagree, please take a look: make.wordpress.org/core/2014/08/29/… Commented Sep 13, 2016 at 19:00
  • @LucasB Random ordering is going to be expensive as it has to load everything in to memory to randomise before it can do the query. Expect super slow page loads and poor scaling with increased traffic.
    – Tom J Nowell
    Commented Sep 13, 2016 at 22:36
  • Thanks for the tip. What I'm doing is getting all Featured Posts in a array (posts_per_page = -1), shuffeling them and getting only the latest 16 regular posts, then I merge it into a single array with 16 posts Commented Sep 14, 2016 at 20:23

3 Answers 3

3

You are right that you can't have WordPress order the retrieved posts randomly. So you'll have to do that yourself after you have retrieved the array of post objects. Let's call that $my_posts.

Because you don't know how many of the posts in that array are featured, you will have to loop through them to split the array. I don't know how exactly you have defined 'featured', but it would look something like this:

$featured_posts = array ();
$nonfeatured_posts = array ();
foreach ($my_posts as $my_post) {
  if (test if featured)
    $featured_posts[] = $my_post
  else
    $nonfeatured_posts[] = $my_post;
// now, let's randomize
shuffle ($featured_posts);
// join the array again
$my_posts = array_merge ($featured_posts,$nonfeatured_posts);
// now you have your array with featured posts randomized

Beware, I couldn't test this code, but I trust you get the point.

0
1

Shuffling a query's results is really as simple as shuffle()ing a query's results.

$query = new WP_Query( //arguments );
shuffle( $query->posts );
0
-2

You can use too:

'orderby'   =>'rand',

this way works for me.

1
  • 1
    No, it won't work. 'orderby' =>'rand' will randomize posts before selecting them. So if you have 100 posts matching query and want to select only 10 of them, you will get random 10. OP wants to select last ten of them and then shuffle them. Commented Jul 25, 2018 at 13:02

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.