PB-2502: The same query ran many times in one request
Paul Bunyan code PB-2502 (Warning): The same query ran many times in one request. What it means and what to do.
On this page
What it means
A request ran 100 or more queries and at least one query shape repeated five or more times.
What to do
Usually a query inside a loop. Fetch the rows once before the loop, or keep the result in a variable or the object cache.
Common causes
- A query inside a loop: one query per product, per post or per menu item (often called the N+1 problem).
- Templates calling
get_post_meta()orget_option()for items one by one when the data wasn't loaded in advance. - A plugin checking the same setting many times without keeping the result.
- No persistent object cache (PB-3002), so repeated lookups always reach the database.
How to find and fix it
- The details show the repeated query shape and which function ran it.
- Fetch the rows once before the loop and look them up in an array inside it.
- Keep results in a variable, or in the object cache with
wp_cache_get()andwp_cache_set(). - If the code belongs to a plugin, send its author the query shape and how many times it ran.
Details
| Code | Severity | Group |
|---|---|---|
| PB-2502 | Warning | Database |
