WooCommerce
WooCommerce Product Filtering Performance: Make Large Catalogues Fast
WooCommerce product filtering performance is mostly a database problem: every active facet adds a `tax_query` join or a `meta_query` scan, and facet-count queries multiply the cost. In my builds I filter on attribute taxonomies, index the columns MySQL actually reads, batch the lookups in the grid, and serve AJAX filters from a small REST endpoint with normalised cache keys. That keeps a large catalogue responsive.
Why filtering gets slow on large catalogues
Filtering looks cheap in the browser and expensive on the server. Each active facet becomes either a `tax_query` join against `wp_term_relationships` and `wp_term_taxonomy`, or a `meta_query` condition against `wp_postmeta`. Add a second facet and MySQL has to intersect two sets; add facet counts and the template may run one query per visible term to show how many products each option would return. That is where a catalogue goes from 300ms to several seconds. I profile on staging with production-sized data, because a store with 200 products hides every one of these costs. Before writing any caching, I want a query count, the slowest statements, and an `EXPLAIN` plan for each filter combination. Caching a slow query only hides it until the cache misses.
- One active facet adds a join or a meta condition to the main product query
- Facet counts multiply queries when computed per term instead of in one pass
- Profile on a staging copy with a production-sized catalogue
- Capture query count, slowest statements and an `EXPLAIN` plan before caching
tax_query versus meta_query: what each one costs
WooCommerce product attributes are taxonomies — `pa_color`, `pa_size`, `pa_brand` — so filtering on them with `tax_query` joins indexed term IDs. Filtering on the same values stored as post meta forces MySQL to read `wp_postmeta` rows and compare `meta_value`, which is a longtext column. I keep filterable properties in attribute taxonomies and use `meta_query` only for genuinely numeric or scalar ranges such as a precomputed sort weight. In `tax_query` I set `field => "term_id"`, `operator => "IN"` and `include_children => false` unless the hierarchy is needed, which avoids extra joins. For meta I stick to `compare => "="` or numeric comparisons; a `LIKE` with a leading wildcard cannot use an index. I also avoid layering `meta_query` and `tax_query` with `relation => "OR"`, because it defeats the selective condition first.
- Filter attributes through `tax_query` on `pa_*` taxonomies, not duplicate post meta
- Use `field => "term_id"`, `operator => "IN"` and `include_children => false`
- Prefer `compare => "="` or numeric comparisons; avoid leading-wildcard `LIKE`
- Avoid OR relations that mix a selective taxonomy with a broad meta condition
Indexing the filter columns and confirming it with EXPLAIN
Indexing is about matching the exact columns MySQL reads. `wp_postmeta` ships with indexes on `post_id` and on `meta_key` as a bounded prefix; newer WordPress versions also add a `meta_key_meta_value` prefix index, so check the schema on the site you are tuning rather than assuming it. A filter on `meta_key = "price_band"` plus `meta_value` benefits from a composite `(meta_key, meta_value(N), post_id)` index, where N is a prefix that fits the server InnoDB limit. For numeric ranges I prefer a dedicated table with typed columns and indexes over casting text in a query. `wp_term_relationships` is already indexed on `term_taxonomy_id` and `object_id`, which is another reason to favour taxonomy filters. I confirm every assumption with `EXPLAIN`: the goal is `type: ref` or `range` using the intended key, not `type: ALL` with `Using temporary; Using filesort`.
- Check actual `wp_postmeta` indexes with `SHOW INDEX` before adding new ones
- Composite `(meta_key, meta_value(prefix), post_id)` for equality filters
- Typed columns in a small custom table for numeric range filters
- `EXPLAIN` should show `ref` or `range` on the intended key, not `ALL`
Avoiding N+1 lookups in the filtered grid
N+1 lookups are the most common cause of a grid that loads fast in the lab and crawls in production. The pattern is a loop that calls `wc_get_product()`, `get_post_meta()` or `wp_get_post_terms()` once per result. With 48 products per page that is 48 hidden query bursts. I avoid it in three ways. First, request IDs only: `wc_get_products()` with `return => "ids"` or `WP_Query` with `fields => "ids"`, `no_found_rows => true`, `update_post_meta_cache => false` and `update_post_term_cache => false`. Second, prime caches in one call with `update_meta_cache()` or `update_object_term_cache()` before the loop. Third, fetch terms in bulk with `wp_get_object_terms()` or `get_terms()` with `object_ids` instead of per product. I keep the grid markup in one template so it is obvious when a new per-item lookup slips in.
- Use `fields => "ids"` or `wc_get_products()` with `return => "ids"`
- Add `no_found_rows => true` and disable post meta and term cache updates
- Prime meta and term caches in one call before the loop
- Fetch terms in bulk with `object_ids` rather than once per product
Designing an AJAX filter endpoint
An AJAX filter should return the smallest possible payload: the product grid, the updated facet counts and the result total — not a whole page. I register a REST route with `register_rest_route()` and a `permission_callback`, rather than hanging another action off `admin-ajax.php`, because the REST request boots less admin code and gives me a clean, cacheable URL. Every incoming parameter is sanitised and whitelisted: taxonomy names come from a fixed array, term IDs go through `array_map( "absint", ... )`, `posts_per_page` is capped, and sort keys are checked against a list. On the client I debounce the input, abort the previous request with `AbortController`, and show a skeleton while the response arrives. The first, unfiltered render stays server-side so the category works without JavaScript.
- `register_rest_route()` with an explicit `permission_callback` for public filters
- Whitelist taxonomy names, run `absint()` on term IDs and cap `posts_per_page`
- Return grid, facet counts and total only; keep the first render server-side
- Debounce input, abort stale requests and show a loading state
Caching filter results without serving stale stock
Caching only works when the key describes the filter state exactly. I normalise the query — sort term IDs, lowercase slugs, drop empty values, fix a canonical sort order — then hash it into a key such as `filter:<cat>:<hash>`. That way `?color=red&size=xl` and `?size=xl&color=red` share one entry. For short-lived, high-churn filter results I use the object cache with a two to five minute expiry; for stable landing combinations I use a transient with a longer TTL. The hard part is invalidation: stock and price changes must not serve a sold-out product. I clear relevant keys on `woocommerce_update_product`, `save_post_product`, `woocommerce_product_set_stock_status` and on term edits. Facet counts get their own longer-lived cache. I never cache cart, checkout or any response that includes a shopper session.
- Normalise and hash filter parameters into one canonical cache key
- Object cache with a short TTL for live results; transients for stable combinations
- Invalidate on product, stock-status and term changes
- Cache facet counts separately from the product grid
What to measure, and when to move beyond MySQL
I am Saif Al Jahan, a WordPress and PHP developer in Mymensingh, Bangladesh (GMT+6), and on webdev-saif.com I treat filter speed as a measured loop. I capture a Query Monitor baseline, run `EXPLAIN` on the top statements, then change one thing and re-measure the same combination. Filter interactions count towards INP, so I watch them in field data from the web-vitals library, not only in Lighthouse. Practical targets are an initial catalogue LCP under 2.5s and a filter response that feels immediate on throttled 4G. When a catalogue passes roughly 50,000 products with many active facets, MySQL joins stop being the right tool; that is the point where I evaluate an indexed search backend such as ElasticPress for faceting. I cover this in the WooCommerce work on this site.
- Baseline with Query Monitor and `EXPLAIN` on production-sized data
- Track filter interactions in field INP data, not only lab scores
- One change at a time, then re-measure the same filter combination
- Consider ElasticPress or a dedicated search index beyond roughly 50,000 products
Frequently asked questions
Why is WooCommerce filtering slow on a large catalogue?
Because each active facet adds a `tax_query` join or a `meta_query` scan, and showing how many products each option would return can add one query per term. On a staging copy with 50,000 products I often see hundreds of queries for one filtered request. I profile first, then remove the repeated lookups before adding any cache.
Should I use tax_query or meta_query for product filters?
Use `tax_query` whenever the property is a WooCommerce attribute taxonomy such as `pa_color`, because term relationships are indexed. Use `meta_query` only for scalar values you control, and keep it to equality or numeric comparisons; a leading-wildcard `LIKE` cannot use an index. Duplicating attributes into post meta and filtering there is usually slower.
How do I stop N+1 queries in the filtered product grid?
Request IDs only, then prime caches in bulk. I use `wc_get_products()` with `return => "ids"`, add `no_found_rows => true`, and call `update_meta_cache()` or `wp_get_object_terms()` once before the loop instead of `get_post_meta()` per product. That turns dozens of hidden queries per page into a handful of predictable ones.
Should I use admin-ajax or the REST API for AJAX filters?
I use a `register_rest_route()` endpoint for public filters. It boots less admin code than `admin-ajax.php`, produces a meaningful URL and is easier to cache. Whitelist the allowed taxonomies, run `absint()` on term IDs, cap `posts_per_page`, and return only the grid and counts. Debounce requests and abort the previous one on the client.
How do I cache filtered results without showing sold-out products?
Build a normalised key from the sorted filter values and give it a short TTL — a few minutes for live results, longer only for stable landing combinations. Clear those keys on product updates, stock-status changes and term edits. Cache facet counts separately, and never cache cart, checkout or any response that contains shopper-specific data.
When should I move filtering off MySQL?
When the catalogue has roughly 50,000 or more products, several facets are active together, and `EXPLAIN` still shows expensive joins or temp tables after indexing and cache priming. At that point an indexed search backend such as ElasticPress does faceting more predictably. I treat that as a measured threshold, not a rule for every store.