Title: WordPress get_posts() returning same post all the time
Last modified: August 20, 2016

---

# WordPress get_posts() returning same post all the time

 *  Resolved [umesh.awasthi](https://wordpress.org/support/users/umeshawasthi/)
 * (@umeshawasthi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/)
 * Hi all,
 * I am just playing around with WordPress and neither I have any idea abt PHP. 
   I am trying to fetch few random posts using get_posts() function of the WordPress
   my code is something like this
 *     ```
       <?php
           args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
           $rand_posts1 = get_posts( $args1);
                   foreach( $rand_posts1 as $randpost1 ) : ?>
                       <?php the_title(); ?>
                            <?php endforeach; ?>
       ```
   
 * But this code is only returning same post all the 12 times and that is the lastest
   post. I am clueless what exactly I am doing wrong.
 * Can any one help me to correct my mistake or point me about my issue.

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

 *  Moderator [Ipstenu (Mika Epstein)](https://wordpress.org/support/users/ipstenu/)
 * (@ipstenu)
 * 🏳️‍🌈 Advisor and Activist
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342707)
 * Read [http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action](http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action)
 * There’s a `$do_not_duplicate` trick 🙂
 *  Thread Starter [umesh.awasthi](https://wordpress.org/support/users/umeshawasthi/)
 * (@umeshawasthi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342711)
 * Thanks for the reply.as new to the platform i even tried this code of piece
 *     ```
       <?php
           args1 = array( 'numberposts' => 12 ,'orderby' => 'rand', 'category' => '7,8');
           $rand_posts1 = get_posts( $args1);
                   foreach( $rand_posts1 as $randpost1 ) : ?>
                       <?php the_title(); ?>
                            <?php endforeach; ?>
       ```
   
 * even specifying the category its still showing the same post the latest one in
   the blog all 12 times
 *  Moderator [Ipstenu (Mika Epstein)](https://wordpress.org/support/users/ipstenu/)
 * (@ipstenu)
 * 🏳️‍🌈 Advisor and Activist
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342715)
 * Yes, because you need to use `$do_not_duplicate`
 * Read [http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action](http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action)
 *  Thread Starter [umesh.awasthi](https://wordpress.org/support/users/umeshawasthi/)
 * (@umeshawasthi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342721)
 * Thanks for the quick reply and it helped me a lot.
    i have to fetch 12 posts 
   so that i can display them in template so is there any way to fetch 12 unique
   random posts in word-press with help of any query?
 *  Thread Starter [umesh.awasthi](https://wordpress.org/support/users/umeshawasthi/)
 * (@umeshawasthi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342737)
 * i tried this code
 *     ```
       <?php
                   $args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
                   global $post;
                   //save the current post
                   $temp=$post;
                   $rand_posts1 = get_posts( $args1);
   
                   foreach( $rand_posts1 as $post ) ://yes this is required, we need $post in setup_postdata
                       setup_postdata($post);    ?>
                       <?php the_title(); ?>
   
                   <?php endforeach; 
   
                   $post=$temp;//restore current page
   
                   ?>
       ```
   
 * it worked smoothly.

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

The topic ‘WordPress get_posts() returning same post all the time’ is closed to 
new replies.

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 5 replies
 * 2 participants
 * Last reply from: [umesh.awasthi](https://wordpress.org/support/users/umeshawasthi/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/wordpress-get_posts-returning-same-post-all-the-time/#post-2342737)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
