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.

2 Answers
2

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *