WP_Query uses the WordPress object cache to store query results. This means if the same query runs again, WordPress can return cached results without querying the database again, improving performance.
The correct parameter to limit posts in WP_Query is posts_per_page. Other options are not recognized and will be ignored.
new WP_Query(['meta_key' => 'color', 'meta_value' => 'blue']);
Querying posts by meta_key and meta_value can be slow because it requires joining the postmeta table, which can be large and unindexed. This slows down the query on big sites.
Using 'tax_query' with multiple taxonomy filters and 'relation' => 'AND' lets WordPress build a single optimized SQL query. Running multiple queries or filtering in PHP is slower.
$query = new WP_Query(['posts_per_page' => 3, 'paged' => 2]);
$count = $query->post_count;
What is the value of
$count if the site has 7 posts total?$query = new WP_Query(['posts_per_page' => 3, 'paged' => 2]); $count = $query->post_count;
With 7 posts total and 3 posts per page, page 1 has posts 1-3, page 2 has posts 4-6 (3 posts), and page 3 has post 7. Since 'paged' is 2, it returns 3 posts, so $count is 3.