WordPress’ WP_Query has orderby=rand parameter to make WP_Query displays random posts1 but there’s no native parameter to randomly displays WP_Query posts output2 inside The Loop. I faced this need and found a workaround for that:

  1. Get a WP_Query object using parameters you want
  2. Assuming you’re using $posts as the variable of your WP_Query object, the posts data that will be used for ‘The Loop’ can be accessed at $posts->posts.
  3. Store and convert $posts->posts into array so you can shuffle the order of the posts
  4. Shuffle the “array”-ed posts
  5. Convert the shuffled posts into object
  6. Assign back the shuffled posts object to $posts->posts
  7. Now you can start outputing the object using “the loop”

Here’s how it looks in form of code:

 


  1. E.g. assuming you have 100 posts, this will display random posts from those 100 posts 

  2. E.g. You want to get 10 latests posts but you want to display it in random order 

Share Your Thought