Support » Developing with WordPress » get_posts help

  • 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 Steve Stern

    (@sterndata)

    Support Team Volunteer

    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.

    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.