Make secondary WP_Query loops cheaper: no_found_rows, cache flags and fields
A default new WP_Query() does more than select posts. It also counts all matching posts for pagination (SQL_CALC_FOUND_ROWS plus SELECT FOUND_ROWS()), loads the meta of all found posts into the cache and loads their terms. For the main query of an archive that is what the template needs. For a widget with five post titles it is work for nothing, on every page view.
Problem: A “recent posts” block in the sidebar shows five titles, but its query also counts all posts and loads meta and terms that the block never uses.
Solution: For lists without pagination set 'no_found_rows' => true; when the template does not read meta or terms, also 'update_post_meta_cache' => false and 'update_post_term_cache' => false; and when only IDs are needed, 'fields' => 'ids'.
The flags
<?php
// Default: posts + count + meta + terms
$recent = new WP_Query( array( 'posts_per_page' => 5 ) );
// Only what a list of titles and links needs
$recent = new WP_Query( array(
'posts_per_page' => 5,
'no_found_rows' => true, // no total count: pagination is impossible, max_num_pages is 0
'update_post_meta_cache' => false, // do not preload post meta
'update_post_term_cache' => false, // do not preload categories and tags
'ignore_sticky_posts' => true, // do not put sticky posts on top
) );
while ( $recent->have_posts() ) {
$recent->the_post();
printf( '<li><a href="%s">%s</a></li>', esc_url( get_permalink() ), esc_html( get_the_title() ) );
}
wp_reset_postdata();
SQL_CALC_FOUND_ROWS makes MySQL find all matching rows even though only five are returned, which on a large posts table is the expensive part. get_posts() sets no_found_rows to true by default; WP_Query does not.
Only IDs
<?php
$ids = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 100,
'fields' => 'ids', // $ids->posts is an array of integers
'no_found_rows' => true,
) );
foreach ( $ids->posts as $id ) {
// pass the ID on, e.g. to a background job
}
With fields => 'ids' WordPress selects only the ID column and does not build WP_Post objects or fill any caches.
When not to turn them off
The cache flags exist to prevent the opposite problem. If the loop reads meta or terms (a price field, the category name, the featured image, which is meta too), the preload fetches them for all posts in one query. Without it every get_post_meta() call queries the database separately: ten posts, ten queries, a classic N+1 problem. How to detect and avoid it is described in avoiding N+1 queries.
The rule:
no_found_rows => true: whenever there is no pagination.update_post_meta_cache => false: only if the loop reads no meta at all, including the thumbnail.update_post_term_cache => false: only if the loop shows no terms and nopost_class()(which adds category and tag classes).
Measure with the Query Monitor plugin, which shows the number and time of queries per component, before and after.
NOTE: These flags speed up the query, not the result. If the same list is shown on every page, caching its HTML or the IDs in a transient for a few minutes saves more than any flag.