Commit 06495140d00 for woocommerce

commit 06495140d007bd39784f13839a45816bbc9833f3
Author: Vladimir Reznichenko <kalessil@gmail.com>
Date:   Tue May 26 11:56:17 2026 +0200

    [Performance] Reduce the number of SQLs on shop and admin products pages. (#65276)

    Adds more cache-priming calls to reduce the number of SQL queries on the products page in the admin area (as a side effect, reduces the number of SQL queries on the shop, cart, and checkout pages).

diff --git a/plugins/woocommerce/changelog/65276-performance-reduce-number-of-sqls-products-listings b/plugins/woocommerce/changelog/65276-performance-reduce-number-of-sqls-products-listings
new file mode 100644
index 00000000000..ec549e685bf
--- /dev/null
+++ b/plugins/woocommerce/changelog/65276-performance-reduce-number-of-sqls-products-listings
@@ -0,0 +1,4 @@
+Significance: minor
+Type: performance
+
+Reduced the number of SQL queries on the shop, cart, and checkout pages in the store, as well as on the products page in the admin area, by adding missing cache priming.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-list-table-products.php b/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-list-table-products.php
index 50e67dc42aa..cc8750af1e2 100644
--- a/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-list-table-products.php
+++ b/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-list-table-products.php
@@ -57,6 +57,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
 		add_filter( 'views_edit-product', array( $this, 'product_views' ) );
 		add_filter( 'get_search_query', array( $this, 'search_label' ) );
 		add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 );
+		add_filter( 'the_posts', array( $this, 'prime_thumbnail_caches' ), 10, 2 );
 		add_action( 'manage_product_posts_custom_column', array( $this, 'add_sample_product_badge' ), 9, 2 );

 		$cogs_controller              = wc_get_container()->get( CostOfGoodsSoldController::class );
@@ -64,6 +65,23 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
 		$this->use_cogs_lookup_column = $this->cogs_is_enabled && $cogs_controller->product_meta_lookup_table_cogs_value_columns_exist();
 	}

+	/**
+	 * Prime featured image caches for product queries to avoid individual queries during rendering.
+	 *
+	 * @since 10.9.0
+	 *
+	 * @param \WP_Post[] $posts Posts from WP Query.
+	 * @param \WP_Query  $query Current query.
+	 * @return array
+	 */
+	public function prime_thumbnail_caches( $posts, $query ) {
+		if ( $query instanceof \WP_Query && 'product' === $query->get( 'post_type' ) ) {
+			update_post_thumbnail_cache( $query );
+		}
+
+		return $posts;
+	}
+
 	/**
 	 * Render blank state.
 	 */
diff --git a/plugins/woocommerce/includes/class-wc-cache-helper.php b/plugins/woocommerce/includes/class-wc-cache-helper.php
index e6cf0946a86..a6c8292406b 100644
--- a/plugins/woocommerce/includes/class-wc-cache-helper.php
+++ b/plugins/woocommerce/includes/class-wc-cache-helper.php
@@ -50,8 +50,16 @@ class WC_Cache_Helper {
 		if ( ! is_blog_installed() ) {
 			return $headers;
 		}
-		$page_ids = array_filter( array( wc_get_page_id( 'cart' ), wc_get_page_id( 'checkout' ), wc_get_page_id( 'myaccount' ) ) );

+		// Optimization note: this is the earliest stage at which we can bulk-prime pages data. As a result, we
+		// prime data for all pages used in is_page checks across the core, even if only a subset is required here.
+		$pages                    = array( 'cart', 'checkout', 'coming_soon', 'myaccount', 'shop', 'terms' );
+		$woocommerce_page_options = array_map( static fn ( string $page ) => sprintf( 'woocommerce_%s_page_id', $page ), $pages );
+		wp_prime_option_caches( $woocommerce_page_options );
+		$woocommerce_page_ids = array_combine( $pages, array_map( static fn ( string $page ) => wc_get_page_id( $page ), $pages ) );
+		_prime_post_caches( array_values( array_filter( $woocommerce_page_ids ) ), false, false );
+
+		$page_ids = array_filter( array( $woocommerce_page_ids['cart'], $woocommerce_page_ids['checkout'], $woocommerce_page_ids['myaccount'] ) );
 		if ( ! is_page( $page_ids ) ) {
 			return $headers;
 		}
diff --git a/plugins/woocommerce/src/Admin/WCAdminHelper.php b/plugins/woocommerce/src/Admin/WCAdminHelper.php
index 9274e042d3f..b6c88709dbe 100644
--- a/plugins/woocommerce/src/Admin/WCAdminHelper.php
+++ b/plugins/woocommerce/src/Admin/WCAdminHelper.php
@@ -147,6 +147,7 @@ class WCAdminHelper {
 		 * @param array $store_pages The store pages array. The keys are the page slugs and the values are the page IDs.
 		 */
 		$store_pages = apply_filters( 'woocommerce_store_pages', $store_pages );
+		_prime_post_caches( array_values( array_filter( $store_pages ) ), false, false );

 		foreach ( $store_pages as $page_slug => $page_id ) {
 			if ( $page_id > 0 && is_page( $page_id ) ) {