How to Speed Up Non-Paginated WordPress WP_Query Loops with no_found_rows
Every time a theme or custom plugin runs new WP_Query() to fetch five related posts, three featured cards, or a navigation menu list, WordPress does extra database work by default: it calculates the total number of matching rows across the entire wp_posts table so it can populate $query->found_posts and $query->max_num_pages for pagination. On a small blog that overhead goes unnoticed, yet on sites with tens of thousands of posts or WooCommerce products, that hidden row-counting query adds up on every uncached page load. Setting 'no_found_rows' => true turns that extra count off when you do not need pagination.
Editorial illustration: pass 'no_found_rows' => true on fixed-count queries, and leave it false on paginated archives.
Summary
- By default,
WP_Queryruns extra counting work (SQL_CALC_FOUND_ROWS/set_found_posts()) to compute total matching rows for pagination. - Passing
'no_found_rows' => trueskips that total count, letting MySQL/MariaDB stop as soon as it gathers the requestedposts_per_pagerows. - As WordPress VIP’s query optimization guide emphasizes, use
'no_found_rows' => trueonly on non-paginated queries (related posts, hero sliders, sidebar widgets, REST/AJAX fixed lists). - Never enable
no_found_rowsglobally viapre_get_postson main archive queries, or pagination links (paginate_links(),next_posts_link()) will break.
What no_found_rows actually changes in SQL
When you instantiate new WP_Query( array( 'posts_per_page' => 5 ) ), WordPress assumes you might render “Page 1 of 42” at the bottom of the loop. To know that 42 exists, MySQL cannot simply stop after finding 5 matching rows—it has to count all matching rows in the table (historically using SQL_CALC_FOUND_ROWS followed by SELECT FOUND_ROWS()).
When you add 'no_found_rows' => true:
$related = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 5,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
)
);
Notice what happens:
- WordPress skips
set_found_posts()completely, leaving$related->found_postsat0and$related->max_num_pagesat0. - MySQL can honor
LIMIT 0, 5and stop scanning as soon as 5 qualifying rows are found. - If your loop only needs post titles and permalinks (no custom fields or category badges), pairing
'no_found_rows' => truewith'update_post_meta_cache' => falseand'update_post_term_cache' => falseavoids extrawp_postmetaandwp_term_relationshipspriming queries too.
When to use no_found_rows => true (and when not to)
Safe places to enable it
- Related posts sections below an article (
posts_per_page => 3or4) - Homepage featured blocks or hero carousels showing a fixed number of items
- Footer or sidebar “Latest News” widgets
- Existence checks (
posts_per_page => 1just to see if at least one matching post exists)
Places you must leave it false
- Main blog archive, category, tag, and search loops where
paginate_links()or “Next Page” buttons depend on$wp_query->max_num_pages - Custom paginated REST endpoints that return
X-WP-TotalandX-WP-TotalPagesheaders - Infinite scroll / “Load More” grids that calculate whether another page exists from
max_num_pages
Step-by-step: Audit custom queries safely
- Test on staging first and measure before changing code. Back up your site (see our WordPress backup restore rehearsal) and inspect slow queries on staging using Query Monitor or your host’s slow SQL log.
- Search your theme and custom plugins for
new WP_Queryandget_posts(). Remember that WordPress’sget_posts()helper already sets'no_found_rows' => trueby default under the hood—it is directnew WP_Query()calls that default tofalse. - Add
'no_found_rows' => trueonly to fixed-limit loops. Check each template where the query runs to confirmmax_num_pagesorfound_postsis not referenced. - Keep meta/term cache flags accurate. Only set
'update_post_meta_cache' => falseif your loop does not callget_post_meta()or ACF’sget_field()inside thewhile ( $query->have_posts() )block—otherwise you will accidentally trade one batch query for N+1 individual queries. - Pair query cleanup with broader WordPress performance hygiene. Check WordPress
wp_optionsautoloaded data and non-blockingwp_enqueue_scriptloading strategies so both database and front-end rendering stay fast.
Common mistakes
- Applying
no_found_rows => trueinsidepre_get_postswithout checking$query->is_main_query(). Even worse, applying it to$query->is_main_query()on/blog/breaks pagination on page 2 and beyond. - Disabling
update_post_meta_cachewhile rendering thumbnails or custom fields.the_post_thumbnail()reads_thumbnail_idpost meta; disabling the meta cache while showing featured images forces WordPress to query_thumbnail_idone post at a time. - Expecting
no_found_rowsto fix an unindexedmeta_query. Skipping row counting helps, yet aLIKE '%keyword%'search acrosswp_postmetawill still scan huge tables slowly unless you rethink the data model or add caching.
Why this matters
Small secondary WP_Query blocks often run on every single page view—header menus, sidebars, and footer widgets included. Removing unnecessary total-row counts keeps database CPU steady during traffic spikes without altering what visitors see.
My Take
Whenever you write new WP_Query() for a fixed list of 3 to 10 items, treat 'no_found_rows' => true as part of your standard boilerplate right next to 'posts_per_page'. Just never touch main archive pagination with it, and keep get_posts() in mind when you only need a simple array of WP_Post objects.
Conclusion
Add 'no_found_rows' => true to custom WP_Query loops that do not use pagination, leave update_post_meta_cache enabled if your template renders featured images or custom fields, and verify both SQL query counts and archive pagination on staging before deploying.