A catalog that works at 200 products has to survive 30,000: variations, product fields and queries.
The case
The “Hoodies” category takes 8 seconds to load. It shows 24 products. Under each card the theme draws color swatches with get_available_variations(), and every hoodie has 40 variations: size × color. That’s 960 variation objects per page.
On the product page, “Related products” runs posts_per_page => -1 with a meta_query and builds an object for every product to pick four. At 200 products nobody noticed. At 30,000 the page takes 6 seconds.
What the database sees
table type key rows Extra
p ALL NULL 30450 Using where
m ref meta_key 29870 Using where
The fixes
<?php
$ids = wc_get_products( [
'status' => 'publish',
'stock_status' => 'instock',
'category' => [ 'hoodies' ],
'limit' => 4,
'return' => 'ids',
] );
A product field the night ERP sync can’t wipe: save it on the admin product form hook, not on save_post (a REST request has an empty $_POST).
<?php
add_action( 'woocommerce_admin_process_product_object', static function ( WC_Product $product ) {
if ( isset( $_POST['_supplier_code'] ) ) { // the WooCommerce product form checks the nonce
$product->update_meta_data( '_supplier_code', sanitize_text_field( wp_unslash( $_POST['_supplier_code'] ) ) );
}
} );
The interview question
How is a variable product built?
Weak answer. It’s a product with several options, like size.
Strong answer. A parent
productpost of type variable with attributes marked for variations. Each variation is a separateproduct_variationpost with its own price, SKU, stock and image. The parent’s price is a range WooCommerce calculates and caches. That’s why the number of variations affects catalog performance.
👉 The full chapter is in the course WooCommerce Development for Middle Devs: Module 3. Product Catalog
🔔 More WooCommerce videos: youtube.com/@4wpdev