WordPress.org

Ready to get started?Download WordPress

Forums

query_posts: Display a random post from the most recent 5 out of 10 posts? (2 posts)

  1. webipsum
    Member
    Posted 2 years ago #

    Hello
    I have a category with 10 posts.
    I want to make a query_posts posts that displays only one, but random and only the five most recent posts.
    I know mount query_posts to:
    View posts only 1,
    View randomly,
    BUT I do not know among the 10 existing posts, working only with the five most recent posts.
    Any tips?
    Thank you.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think this will do what you want:

    $args = array(
       'cat' => 55,          // Your category id here
       'posts_per_page' => 5,
       'orderby' => 'rand',
       'ignore_sticky_posts' => 1
    );
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post();
          // Your code to display the post here
          break;  // Exit loop after first post
       endwhile;
    endif;

Topic Closed

This topic has been closed to new replies.

About this Topic