dangerChihuahua007
dangerChihuahua007

Reputation: 20895

How can I show only a certain number of posts in a query?

I am trying to query for the 3 most recent posts in the home page of my website powered by Wordpress:

    wp_reset_postdata();
    wp_reset_query();
    query_posts('showposts=3');
    rewind_posts();
    while (have_posts()) : the_post();
        // do stuff.
        the_title();
    endwhile;

However, 7 posts are being displayed. Furthermore, not all of these 7 posts are the latest posts. Above this query, there are other queries.

As you see, I have tried reseting the post data and the query data to no avail. What other factors could affect the posts retrieved?

Upvotes: 0

Views: 122

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10356

Be sure that you don't put that code in another have_posts loop. This should work:

query_posts( array( 'posts_per_page' => 3, 'orderby' => 'date', 'order' => 'DESC' ) );
    while (have_posts()) : the_post();
        // do stuff.
        the_title();
    endwhile;

Upvotes: 2

Related Questions