Title: Random Sticky Posts
Last modified: August 19, 2016

---

# Random Sticky Posts

 *  Resolved [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/)
 * I’m using the following code to bring in 3 sticky posts into my sidebar. Everything
   is working okay with the exception of the “orderby” parameter. I’d like these
   posts to show randomly but in its current state they are only showing by date(
   newest first).
 * Any help would be greatly appreciated.
 *     ```
       <?php
       $cat = get_query_var('cat');
       $sticky=get_option('sticky_posts');
       $args=array(
         'showposts' => 3,
         'post__in' => $sticky,
         'orderby'=> rand,
         'category__in'=>array($cat)
          );
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <div class="sticky">
           <small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
           <p class="descr"><?php echo substr(strip_tags($post->post_content), 0, 200)."...";?></p></div>
           <?php
           //this is necessary to show the tags
           global $wp_query;
           $wp_query->in_the_loop = true;
         endwhile;
       }
       ?>
       ```
   

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526346)
 * Try adding the caller_get_posts parameter so that sticky posts are not handled
   separately:
 *     ```
       $args=array(
         'showposts' => 3,
         'post__in' => $sticky,
         'orderby'=> rand,
         'category__in'=>array($cat),
         'caller_get_posts' => 1
          );
       ```
   
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526353)
 * Thanks – tried that but it doesn’t seem to have any effect. They still seem to
   show by date and not randomly.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526355)
 * Can you post what is in the SQL query? That might help to see what is going on.
   Just copy and post the SQL output from this:
 *     ```
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
          global $wp_query;
          echo '<p>';print_r($wp_query->request);echo '</p>';
       ```
   
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526360)
 * Apologies, but where do I put the code that you’ve mentioned?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526379)
 * This is the same $my_query that you showed in your first sample code above. I
   just added the two lines after `if( $my_query->have_posts() ) {`
 * Change this:
 *     ```
       <?php
       $cat = get_query_var('cat');
       $sticky=get_option('sticky_posts');
       $args=array(
         'showposts' => 3,
         'post__in' => $sticky,
         'orderby'=> rand,
         'category__in'=>array($cat)
          );
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * to this:
 *     ```
       <?php
       $cat = get_query_var('cat');
       $sticky=get_option('sticky_posts');
       $args=array(
         'showposts' => 3,
         'post__in' => $sticky,
         'orderby'=> rand,
         'category__in'=>array($cat)
          );
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
          global $wp_query;
          echo '<p>';print_r($wp_query->request);echo '</p>';
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * It will print out the SQL from the query ahead of your posts. Just copy from 
   the screen and paste it back here.
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526382)
 * Ah, sorry – it’s getting late and my brain is a little fried!
 * Here’s what it outputted:
 * SELECT SQL_CALC_FOUND_ROWS distinct mYh7Tf_posts.* FROM mYh7Tf_posts INNER JOIN
   mYh7Tf_term_relationships ON (mYh7Tf_posts.ID = mYh7Tf_term_relationships.object_id)
   INNER JOIN mYh7Tf_term_taxonomy ON (mYh7Tf_term_relationships.term_taxonomy_id
   = mYh7Tf_term_taxonomy.term_taxonomy_id) WHERE 1=1 AND mYh7Tf_term_taxonomy.taxonomy
   = ‘category’ AND mYh7Tf_term_taxonomy.term_id IN (‘4′, ’25’, ’26’, ’27’, ’28’,’
   29’, ’30’, ’31’, ’32’) AND mYh7Tf_posts.post_type = ‘post’ AND (mYh7Tf_posts.
   post_status = ‘publish’ OR mYh7Tf_posts.post_status = ‘private’) GROUP BY mYh7Tf_posts.
   ID ORDER BY mYh7Tf_posts.post_date DESC LIMIT 0, 10
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526391)
 * Alright, if you look at that code you can see that the ORDER BY clause is on 
   post_date. The ‘rand’ is being ignored for some reason.
 * It may work to force the use of rand() by inserting lines at the first of the
   code you posted, like this:
 * Change this:
 *     ```
       <?php
       $cat = get_query_var('cat');
       ```
   
 * to this:
 *     ```
       <?php
       function mam_posts_orderby ($orderby) {
          global $mam_global_orderby;
          if ($mam_global_orderby) $orderby = $mam_global_orderby;
          return $orderby;
       }
       add_filter('posts_orderby','mam_posts_orderby');
       global $mam_global_orderby;
       $mam_global_orderby = 'rand()';
       $cat = get_query_var('cat');
       ```
   
 * You can remove the two lines you put in to print out the SQL.
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526416)
 * Spot on. Thank you so much for all your help with this. Really appreciated.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526433)
 * Glad it worked for you. Now, please use the dropdown at top right to mark this
   topic ‘Resolved’.

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

The topic ‘Random Sticky Posts’ is closed to new replies.

## Tags

 * [Sticky posts](https://wordpress.org/support/topic-tag/sticky-posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/random-sticky-posts/#post-1526433)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
