Title: Rand order inst work ?
Last modified: August 20, 2016

---

# Rand order inst work ?

 *  [rakisuy](https://wordpress.org/support/users/rakisuy/)
 * (@rakisuy)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/rand-order-inst-work/)
 * Hi, I’m trying of show a relationships posts, I use this code:
 *     ```
       <?php
       						$categorias = array();
       						$categories = get_the_category();
       						foreach($categories as $categoria){
   
                                   array_push($categorias, $categoria->cat_ID);
       						}
       						?>
                               <div id="similares">
                               	<h3 class="entry-title" style="margin: 20px 0 10px 0; width: 540px;">Relacionados</h3>
   
       							<?php
       							$args = array('showposts' => 3, 'orderby' => 'rand', 'category__and' => $categorias);
                                   print_r($args);
       							$relac = new WP_Query($args);
                                   if($relac->have_posts()):
                                       while($relac->have_posts()): $relac->the_post(); ?>
       									<div class="product-wrap">
                                           <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo crop(get_the_title(),45,false); ?></a></h3>
                                           <div class="image"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php $imagenurlthum = get_thumbnail_src($post->ID); if(!empty($imagenurlthum)): ?><img height="115" src="<?php bloginfo('template_directory') ?>/timthumb.php?src=<?php echo $imagenurlthum; ?>&w=153" title="<?php the_title() ?>" alt="<?php the_title() ?>" /><?php else: ?><img height="115" src="<?php bloginfo('template_directory') ?>/timthumb.php?src=<?php bloginfo('template_directory') ?>/images/logo_login.gif&w=153" title="<?php the_title() ?>" alt="<?php the_title() ?>" style="margin: 25px 0 0" /><?php endif; ?></a></div>
                                           <div class="excerpt"><?php echo crop(strip_tags(get_the_excerpt()),80); ?></div>
                                           <div class="price"><?php echo get_post_meta($post->ID,'Precio',true) ?></div>
                                       </div>
                                       <?php
                                       endwhile;
                                   endif;
                                   ?>
       ```
   
 * If I use category parameters, will display a correct post, but never different
   posts, without variation !

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365305)
 * To get the mySQL random function, you need to include the parenthesis: ‘RAND()’
   otherwise you are telling SQL to order the results by the column named ‘rand’.
 * I have heard ORDERBY RAND() is very inefficient and you’re better off getting
   all possible results and randomly picking 3 posts by index number, though by 
   limiting the query to 3 posts perhaps that is not so bad. It’d be interesting
   to time each approach.
 *  Thread Starter [rakisuy](https://wordpress.org/support/users/rakisuy/)
 * (@rakisuy)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365349)
 * I’m using the class WP_Query, and his doc for do it !
    How do you say that I 
   can use random function ?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365374)
 * Oops! You are right, I misunderstood how orderby works in the WP_Query context,
   my apologies. I cannot explain why the results repeat, your usage appears correct.
 * Since it’s inefficient, and does not work anyway, you could query for all posts
   in the categories you want, then instead of a while loop, do something like
 *     ```
       global $post;
       for ($i = 1; $i <= 3; $i++) {
         $post = $relac[rand(0,count($relac)-1)];
         // do the usual title-permalink-content-etcetera stuff
       }
       ```
   
 * There is a chance of duplicate posts this way, you could add code to push used
   post ids into an array and retry random posts if the current id is in the array
   already.
 *  [webmasterobservatoriobioetica](https://wordpress.org/support/users/webmasterobservatoriobioetica/)
 * (@webmasterobservatoriobioetica)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365502)
 * Hi, I have a similar problem.
 * I’m making a page, and I need to order some posts belonging to two specific categories,
   for this, I looked around, and I have to use the category__and. So much perfect.
 * The problem is that if I use the post category__and not sorted from newest to
   oldest, it is sorted by date of creation, the first post created the first and
   last end.
 * code used:
 * CATEGORY__IN = WORK:
 *     ```
       if( trim( $pagination ) == 'true' ) {
       	$paged = mysite_get_page_query();
       	$blog_query->query(array(
       		'post__in' => $post_in,
       		'category__in' => $category_in,
       		'tag__in' => $tag_in,
       		'post_type' => 'post',
       		'posts_per_page' => $showposts,
       		'paged' => $paged,
       		'offset' => $offset,
       		'ignore_sticky_posts' => 1,
       'orderby' => 'date',
       'order' => 'DESC',
       	));
   
       } else {
   
       	$blog_query->query(array(
       		'post__in' => $post_in,
       		'category__in' => $category_in,
       		'tag__in' => $tag_in,
       		'post_type' => 'post',
       		'showposts' => $showposts,
       		'nopaging' => 0,
       		'offset' => $offset,
       		'ignore_sticky_posts' => 1,
       'orderby' => 'date',
       'order' => 'DESC',
       	));
       }
       ```
   
 * WITH CATEGORY__AND = NOT WORK
 *     ```
       if( trim( $pagination ) == 'true' ) {
       	$paged = mysite_get_page_query();
       	$blog_query->query(array(
       		'post__in' => $post_in,
       		'category__and' => $category_in,
       		'tag__in' => $tag_in,
       		'post_type' => 'post',
       		'posts_per_page' => $showposts,
       		'paged' => $paged,
       		'offset' => $offset,
       		'ignore_sticky_posts' => 1,
       'orderby' => 'date',
       'order' => 'DESC',
       	));
   
       } else {
   
       	$blog_query->query(array(
       		'post__in' => $post_in,
       		'category__and' => $category_in,
       		'tag__in' => $tag_in,
       		'post_type' => 'post',
       		'showposts' => $showposts,
       		'nopaging' => 0,
       		'offset' => $offset,
       		'ignore_sticky_posts' => 1,
       'orderby' => 'date',
       'order' => 'DESC',
       	));
       }
       ```
   
 * If I use category__in, all right, if I use category__and, not working 🙁
 * Can you help me?? Thanks
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365504)
 * You are saying the order parameter acts as if it were ‘ASC’ only when you use
   the ‘category__and’ argument, though you clearly are requesting the order to 
   be ‘DESC’?
 * This is quite strange. The category__and code does nothing with order parameters
   at all in source code. Perhaps a plugin or your theme is filtering queries for
   another purpose and your query is getting accidentally caught in that filter?
   Try deactivating plugins and even switching themes, just to see if this odd behavior
   goes away.
 * You can also examine the query var ‘order’ to ensure it is parsed correctly. 
   Or try hooking (with a high numbered priority, execute last parameter) the ‘posts_request’
   filter to see that the actual mySQL query is as you expect. If all this checks
   out, I’d have to say it’s some SQL quirk rather than WP.
 * If you are desperate to get something working, change the order parameter to 
   ASC for compatibility, then use array_reverse() to get the correct order.
 * BTW, in the future, we prefer you start your own topic when you have a question,
   even if similar to someone else’s. It avoids confusion and your question is more
   likely to be seen by more people, increasing your chances of getting a good answer.
   Good luck tracking down the cause of this mystery.
 *  [webmasterobservatoriobioetica](https://wordpress.org/support/users/webmasterobservatoriobioetica/)
 * (@webmasterobservatoriobioetica)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365505)
 * Ok, sorry,
 * I changed this in the wp-includes/query.php line 2283 (WordPress V3.5) and now
   it’s work correctly… it’s correct?
 * Change this
 *     ```
       if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
       $groupby = "{$wpdb->posts}.ID";
       }
       ```
   
 * for this:
 *     ```
       if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
       //$groupby = "{$wpdb->posts}.ID";
       }
       ```
   
 * Thanks

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Rand order inst work ?’ is closed to new replies.

## Tags

 * [entries](https://wordpress.org/support/topic-tag/entries/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [wpquery](https://wordpress.org/support/topic-tag/wpquery/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 3 participants
 * Last reply from: [webmasterobservatoriobioetica](https://wordpress.org/support/users/webmasterobservatoriobioetica/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/rand-order-inst-work/#post-3365505)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
