Title: Random posts in sidebar
Last modified: August 19, 2016

---

# Random posts in sidebar

 *  [ravasthi](https://wordpress.org/support/users/ravasthi/)
 * (@ravasthi)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/random-posts-in-sidebar/)
 * Hi. Based on the [get_posts() documentation](http://codex.wordpress.org/Template_Tags/get_posts)
   on the Codex, I’m trying to create a simple sidebar widget to list 5 random posts
   from the blog’s archives. My code is as follows:
 *     ```
       function widget_from_the_archives($args) {
         extract($args);
       ?>
         <?php echo $before_widget; ?>
         <?php echo $before_title . "From the archives" . $after_title; ?>
         <ul>
           <?php
             $rand_posts = get_posts("numberposts=5&orderby=rand");
             foreach( $rand_posts as $post ) :
               setup_postdata($post);
           ?>
             <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
           <?php endforeach; ?>
           </ul>
         <?php echo $after_widget; ?>
       <?php
       }
       ```
   
 * Problem is, this code selects the same post 5 times. Am I doing something wrong?
   The code seems pretty straightforward, but it’s not working for me. Please let
   me know if I’m missing something obvious.
 * Thanks in advance,
    Richa Avasthi

The topic ‘Random posts in sidebar’ is closed to new replies.

## Tags

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

 * 0 replies
 * 1 participant
 * Last reply from: [ravasthi](https://wordpress.org/support/users/ravasthi/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/random-posts-in-sidebar/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
