How to limit the number of posts that WP_Query gets?

I have been researching on Google and WPSE and the only thing I see repeatedly is to use showposts, that is deprecated.

I am familiar with WP_Query, and I thought that if I set posts_per_page to my limit (ie. 5), and nopaging to true, it would become to something like "Ok, I'll give you only 5 posts". But this doesn't work.

enter image description here

How can I do this?

Solutions

Ok , lets you have post type called 'blog_posts' , and you want to fetch 5 posts of that post type . Here is what you need to do

$args = array(
        'post_type' => 'blog_posts',
        'posts_per_page' => '5',
);


$query = new WP_Query($args);

The above query will return 5 posts of type 'blog_posts' , if it is not a custom post type , then just replace like this 'post_type' => 'posts', if you want to fetch all posts then replace like this 'posts_per_page' => '-1', , for more details WP Query

I think that now I understand what you are trying to do. When you run a custom query with WP_Query and set the limit to get only 5 posts per page, only 5 posts will be retrieved by the query and that query will only hold 5 posts, BUT for the sake of pagination, WP_Query still runs through the whole database and counts all the posts that matches the criteria of the query.

That can be seen when you look at the $found_posts and $max_num_pages properties of the query. Lets take an example:

You have 20 posts belonging to the default post type post. You only need the latest 5 posts without pagination. Your query looks like this

$q = new WP_Query( 'posts_per_page=5' );
  • var_dump( $q->posts ) will give you the latest 5 posts as expected
  • echo $q->found_posts will give you 20
  • echo $q->max_num_pages will give you 4

The impact of this extra work is minimal on sites with only a few posts, but this can gt expensive if you are running a site with hundreds or thousands of posts. This is a waste of resources if you are only ever going to need the 5 latest posts

There is an undocumented parameter called no_found_rows which uses boolean values which you can use to make your query bail after it found the 5 posts you need. This will force WP_Query not to look for any more posts mathing the criteria after it has retrieved the amount of posts queried. This parameter is already build into get_posts, that is why get_posts is a bit faster than WP_Query although get_posts uses WP_Query

Conclusion

In conclusion, if you are not going to use pagination on a query, it is always wise to 'no_found_rows=true' in your query to speed things up and to save on wasting resources.

After the conversation with @Pieter Goosen on the comments of the question, I think I can answer the question and explain my mistake.

The key is that found_posts was confussing me. I thougth that, that number is the posts retrieved but is not. It is the number of posts that match the criteria. It's like the WP_Query had 2 parts: one for finding (all) the posts, and other for fetching the content, when it checks for the pagination parameters. So we have the $post_count property that is the number of posts fetched (Codex says The number of posts being displayed), that of course is equal to the number on posts_per_page parameter, and the number of items on the $posts array property.

So WP_Query is not doing any useless work, as I thought ^^

Hope this helps others!

Tags: Posts / Wp Query / Query

Similar questions

Alter code to get all posts and show in dropdown list, right kown gets only 5 posts
I know little about php I have tryed to puzzel my way throught this but have not got any where. I need this dropdown list show all posts right now it shows only 5 posts. Thank you Edit: This is the only other bit of code
Displaying the number of posts in a custom loop (without including the number of posts of a loop below)?
I have this custom loop: I would like to retrieve the number of posts (only in this loop). (The is another loop below). Any suggestions?
WordPress Get Number of Post + all Number of all Posts (example No. 3 of 19 Posts)
I would like to show the actual number of a post + all like: „No. 3 of 19 Posts“. I've tried with – it shows all posts but I don't how I show the current post number.
Limit number of posts on a custom category template - breaking pagination
The following code was mentioned from one of the commentors, however it is breaking the pagination. Any corrections please
Limit the number of posts in query_posts function with custom post types
I am using the code below to display posts defined as a custom-post-type and filtered by a custom-taxonomy of 'england'. I've tried using 'posts_per_page=5' in the query_posts function but this brings up a completely different set of posts from one of my post categories of type 'news'. When I remove the posts-per-page from the query, it returns the...
How to limit the number of posts a user can publish based on user type
I am creating a member only wordpress site that will restrict the number of custom posts a user can publish (Custom post type = contacts) based on their role. I will have 4 roles: Role 1 = 25 posts; Role 2 = 150 posts; Role 3 = 300 posts; Role 4 = unlimited posts Is this possible to do with WP hooks?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.