Title: get_posts help
Last modified: December 22, 2018

---

# get_posts help

 *  [masterross](https://wordpress.org/support/users/masterross/)
 * (@masterross)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/get_posts-help-2/)
 * Hi guys,
 * I’m trying to update my custom page.
    Currently I have:
 *     ```
       $posts = get_posts('orderby=rand&numberposts=5');
       foreach ($posts as $post) : setup_postdata( $post ); ?>
       ```
   
 * and this shows random 5 posts.
    But I want to show random 5 posts from the last
   100 posts. How to do it?
 * Thanks in advance!

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 5 months ago](https://wordpress.org/support/topic/get_posts-help-2/#post-11020551)
 * Get 100 posts in random order and put a counter on your loop to show only the
   first 5. Not terribly efficient, but it will work.
 *  Thread Starter [masterross](https://wordpress.org/support/users/masterross/)
 * (@masterross)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/get_posts-help-2/#post-11021238)
 * Hi,
    Thx for the hint! I did it this way:
 *     ```
       $posts = get_posts('numberposts=1000');
       shuffle($posts);
       $posts_five=array_slice($posts, 0, 5, true);
       foreach ($posts_five as $post) : setup_postdata( $post ) ?>
       ```
   
 * Not sure how efficient it is ’cause I’m not programmer, but does the job.
    Thanks
   once again!

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

The topic ‘get_posts help’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [masterross](https://wordpress.org/support/users/masterross/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/get_posts-help-2/#post-11021238)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
