Faster WooCommerce product archives: lookup tables and cached queries
A shop category with a few thousand products, price sorting and attribute filters can take seconds to render. WooCommerce has its own tables to make these queries cheaper, the product meta lookup table and the attributes lookup table, but they help only when they are filled and turned on. For your own product queries, caching the IDs saves the rest.
Problem: Category pages with filters by colour and size are slow, Query Monitor shows long queries on wp_postmeta and wp_term_relationships, and a “featured products” block on the home page repeats the same query on every view.
Solution: Regenerate the product lookup tables, enable “Use the product attributes lookup table for catalog filtering” in WooCommerce, and cache custom product queries as lists of IDs in a transient with a version number that changes when a product is saved.
The lookup tables
wc_product_meta_lookup (since WooCommerce 3.6) stores price, stock and rating of each product in normal columns, so sorting by price and filtering by stock do not need joins on wp_postmeta. wc_product_attributes_lookup (since 6.1) does the same for the layered navigation filters.
wp wc tool run regenerate_product_lookup_tables --user=1 wp wc tool run regenerate_product_attributes_lookup_table --user=1 wp option get woocommerce_attribute_lookup_enabled
In WooCommerce, Settings, Products, Advanced, enable “Use the product attributes lookup table for catalog filtering” after the table has been filled (the regeneration runs in the background with Action Scheduler; the setting page shows when it is finished). Products imported with SQL instead of the WooCommerce API do not appear in the tables until they are regenerated.
Cache your own queries as IDs
function ha_featured_product_ids( $limit = 8 ) {
$version = (int) get_option( 'ha_products_cache_version', 1 );
$key = "ha_featured_{$limit}_v{$version}";
$ids = get_transient( $key );
if ( false === $ids ) {
$ids = wc_get_products( [
'status' => 'publish',
'featured' => true,
'visibility' => 'catalog',
'limit' => $limit,
'return' => 'ids',
] );
set_transient( $key, $ids, HOUR_IN_SECONDS );
}
return $ids;
}
function ha_bump_products_cache() {
update_option( 'ha_products_cache_version', (int) get_option( 'ha_products_cache_version', 1 ) + 1, false );
}
add_action( 'woocommerce_update_product', 'ha_bump_products_cache' );
add_action( 'woocommerce_product_set_stock_status', 'ha_bump_products_cache' );
Cache the IDs, not the product objects: the list is small, and loading the products by ID with wc_get_products( [ 'include' => $ids, 'limit' => -1 ] ) or [products ids="..."] is fast, because WooCommerce primes the post and meta caches in one go. A new version number makes all old keys unused at once; they expire by themselves.
Measure
Query Monitor (Queries by Component, Duplicate Queries) shows which plugin or template runs the slow and repeated queries. Plans of single queries: EXPLAIN for slow MySQL queries.
NOTE: Avoid query hints such as STRAIGHT_JOIN in posts_clauses: they fix one plan for one data size and make it worse when the catalogue changes. With a persistent object cache (Redis), the transients above live in memory, and WooCommerce caches much more by itself; see the object cache and Redis.