2

How to limit WP_Query to grab only let’s say 5 results? What I mean is actually only 5 results rather than 5 posts per page.

This

How to limit the number of posts that WP_Query gets?

provides some insight and advices to use no_found_rows=true but do you use it with posts_per_page or do you need to limit results somewhere else?

In my case (search) get_posts doesn’t work as I need to provide a search query.

1

As you already mentioned, to limit the number of posts retrieved by the query, you can use posts_per_page.

This argument works both for get_posts and WP_Query(), for example:

$args = array(
    'posts_per_page' => 5,

);
$posts_array = get_posts( $args );

And as stated in the link you provided, no_found_rows = true will end the query after it reached its criteria. It's totally optional, and not very common.

  • Any idea of performance gains with no_found_rows = true? Is it worth it? – Runnick Dec 19 '17 at 16:09
  • Worth it against how many posts? How complex queries? – Jack Johansson Dec 19 '17 at 16:55
  • Queries are search queries and category / author queries. Posts... it depends. If I rephrase my comment as “how complex should be a query and how many posts you should have to gain from limiting that way”? – Runnick Dec 19 '17 at 16:58
  • A query that contains tax_query can be heavy. At that point, yes it might be worth it. But querying the last 10 posts or so? No, not noticeable. – Jack Johansson Dec 19 '17 at 17:00
1

What you are looking for isn't a parameter of WP_Query, it is the filter post_limits.

This filter applies to a query's LIMIT clause before the query is sent to the database, allowing you to define a new query LIMIT.

You can find more details here: https://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits

Depending on your specific case, you have two ways to solve it:

  1. Use a condition inside the function, like the example in the codex.
  2. Don't use any condition, just return LIMIT 0, 5. So add the filter right before your query and remove it right after.

UPDATE: Added a complete example for the second solution.

Declare the functions in functions.php

function custom_get_posts_limit(){
    return 'LIMIT 0, 5';
}

Then use it wherever you want

add_filter( 'post_limits', 'custom_get_posts_limit' );

// Add all your others args
get_posts( array( 'suppress_filters' => false) );

remove_filter( 'post_limits', 'custom_get_posts_limit' );
  • This will only work for main query. It’s valuable addition though, thanks. – Runnick Dec 19 '17 at 16:10
  • 1
    @Runnick that's not correct, you can apply the second solution wherever you want, also with the function get_posts(). You just need to set the parameter suppress_filters to false. I am going to add a complete example to the reply right now. – Antonio Dec 19 '17 at 21:49
  • but you can have only one filter/limit per all queries? – Runnick Dec 22 '17 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.