Title: wp_query orderby random not working
Last modified: August 20, 2016

---

# wp_query orderby random not working

 *  [hyperhead](https://wordpress.org/support/users/hyperhead/)
 * (@hyperhead)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/)
 * Can’t for the life of me get the orderby to be random in this wp_query. ‘rand’
   not working at all.
 * Can anyone see why this code is still producing a list of posts in standard reverse
   chronological order instead of randomized mix?
 *     ```
       function homex_top10_loop() {
       	$args = array(
       		'posts_per_page' => 10,
       		'post_type' => 'post',
       		'cat' => 4,
       		'orderby' => 'rand'
       	);
       	//print_r($args);
       	global $wp_query;
       	$wp_query = new WP_Query( $args );
       ```
   

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/page/2/?output_format=md)

 *  Moderator [t-p](https://wordpress.org/support/users/t-p/)
 * (@t-p)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696642)
 * see if info in this codex helps: [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [hyperhead](https://wordpress.org/support/users/hyperhead/)
 * (@hyperhead)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696643)
 * thanks t-p… that’s where i found the sample code this is based on.
 * for the life of me, i could swear this snippet is following the EXACT protocols
   on that codex page, unless my eyes have gone numb to something wrong here. ???
 * <sigh>
 *  [Bill Dennen](https://wordpress.org/support/users/xyzzy/)
 * (@xyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696722)
 * For what it’s worth, I am running into the same exact problem.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696723)
 * That is strange because the core query code is using:
 *     ```
       $orderby = 'RAND()';
       ```
   
 * I use this in a template page of posts to reverse the posts in a list:
 *     ```
       <?php query_posts($query_string . "&order=ASC"); ?>
       ```
   
 * So it should reorder them as well.
 * Just a suggestion remove it from the arguments and try this just before the have_posts(),
   it should apply a filter and return the lines and query the returned recordset.
 *     ```
       <?php query_posts($query_string . "&orderby=rand"); ?>
   
       <?php if ( have_posts() ) : ?>
       ```
   
 * **EDIT:**
    Tested the above in a twenty eleven child theme’s archive.php and 
   when I viewed the monthly archive the posts were random!
 * HTH
 * David
 *  [Bill Dennen](https://wordpress.org/support/users/xyzzy/)
 * (@xyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696724)
 * Thanks. This isn’t working for me either. Maybe part of the problem is that I
   am actually doing this outside of WordPress. My PHP script has this to include
   WordPress:
 * `require('/home/dir/public_html/wp-load.php');`
 * Everything else works fine, except for the sorting. Weird.
 *  [Jason Paul](https://wordpress.org/support/users/jasontrasaterracom/)
 * (@jasontrasaterracom)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696736)
 * I’m having quite a time with this too. Nothing I try with WP_Query will output
   a list of random posts. Any additional advice? Here’s What my code looks like:
 *     ```
       <?php
       global $wp_query;
       query_posts(
       	array_merge(
       		$wp_query->query,
       		array(
       		//'order' => 'ASC',
       		//'orderby'=>'rand', 'none',
       		'posts_per_page' => '3',
       		'tax_query'	=> array(
               array(
                   'taxonomy'  => 'category',
                   'field'     => 'slug',
                   'terms'     => array( 'featured'),// exclude media posts in the news-cat custom taxonomy
                   'operator'  => 'IN')
                   ),
   
       		)
       	)
       ); ?>
   
       <?php if (have_posts ( $query_string . "&orderby=rand" )) : while (have_posts()) : the_post(); ?>
       ```
   
 *  [Jason Paul](https://wordpress.org/support/users/jasontrasaterracom/)
 * (@jasontrasaterracom)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696737)
 * Forgot to add notify on this.
 *  [phases](https://wordpress.org/support/users/phases/)
 * (@phases)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696738)
 * running into the same issue here. orderby=rand is simply not working.
 * Keeping an eye out here for a possible solution.
 *  [Jason Paul](https://wordpress.org/support/users/jasontrasaterracom/)
 * (@jasontrasaterracom)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696739)
 * I was developing a site on WP Engine. They actually disable the Orderby Rand 
   Function. If you happen to be using WP Engine, in the dashboard area they put
   in the WP Admin go there scroll down and make sure the Orderby Rand function 
   is not disabled. You would not believe what I went through before I deduced that
   it was the host causing the problem.
 *  [phases](https://wordpress.org/support/users/phases/)
 * (@phases)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696740)
 * Jason, you are a gem! This was a WP Engine site and I was just about to loose
   my mind trying to figure out what was causing it.
 * THANK YOU!
 *  [melster](https://wordpress.org/support/users/melster/)
 * (@melster)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696742)
 * **t-p** you help me a lot! thanks
 *  [melster](https://wordpress.org/support/users/melster/)
 * (@melster)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696743)
 * here is the code that i used..
 * just want to share..
 * `<?php $loop = new WP_Query( array( orderby => 'rand', 'post_type' => 'testimonials','
   posts_per_page' => 1 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?
   > <?php the_permalink();?>
 *  [Josh Fialkoff](https://wordpress.org/support/users/nooozeguy/)
 * (@nooozeguy)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696744)
 * Thanks! It is indeed a server-side setting.
 *  [Devin Walker](https://wordpress.org/support/users/dlocc/)
 * (@dlocc)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696747)
 * I’m using the same code as Melster and not seeing a random post. I’ll keep poking
   around and see what I can find. For now, here’s my code:
 *     ```
       <?php $args = array(
           'orderby' => 'rand',
           'post_type' => 'testimonial',
       	'posts_per_page' => 1
       );
       // The Query
       $the_query = new WP_Query( $args );
   
       // The Loop
       if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
   
       <div class="testimonial-slide">
         <blockquote>
           <p class="testimonial-title"><?php echo get_post_meta($post->ID, 'testimonial_heading', true); ?></p>
           <?php the_content(); ?>
           <p class="attribution"><?php echo get_the_title(); ?></p>
         </blockquote>
       </div>
       <?php endwhile; endif; ?>
       ```
   
 *  [Devin Walker](https://wordpress.org/support/users/dlocc/)
 * (@dlocc)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/#post-2696748)
 * OK – so it turns out I was using the Post Types Order plugin which removes the
   ability for random ordering unless you place:
 * `remove_all_filters('posts_orderby');`
 * before your query… like so:
 *     ```
       remove_all_filters('posts_orderby');
        $args = array(
           'orderby' => 'rand',
           'post_type' => 'testimonial',
       	'posts_per_page' => 1
       );
       // The Query
       $the_query = new WP_Query( $args );
       ```
   
 * I got this solution from: [How To Get Random Posts From WP Query](http://designerfied.com/1361/how-to-get-random-posts-from-wp-query.html).
   I hope this helps someone!

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/page/2/?output_format=md)

The topic ‘wp_query orderby random not working’ is closed to new replies.

## Tags

 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [rand](https://wordpress.org/support/topic-tag/rand/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 10 participants
 * Last reply from: [lkraav](https://wordpress.org/support/users/lkraav/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/wp_query-orderby-random-not-working/page/2/#post-2696751)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
