Commit ab791ca7836 for woocommerce

commit ab791ca78369ce28160149052b379ab3144b1483
Author: Alba Rincón <albarin@users.noreply.github.com>
Date:   Thu Apr 9 08:16:02 2026 +0200

    Prime attachment post caches in products REST get_images() (#64070)

    * Prime attachment post caches in products REST controllers' get_images()

    * Add changefile(s) from automation for the following project(s): woocommerce

    ---------

    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
    Co-authored-by: Vladimir Reznichenko <kalessil@gmail.com>

diff --git a/plugins/woocommerce/changelog/64070-prime-caches-products-rest-images b/plugins/woocommerce/changelog/64070-prime-caches-products-rest-images
new file mode 100644
index 00000000000..64cfe662bc4
--- /dev/null
+++ b/plugins/woocommerce/changelog/64070-prime-caches-products-rest-images
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Reduce the number of database queries when listing products via the REST API by priming attachment post caches in `get_images()`.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php
index db7ecd45fe5..d742f7c2d87 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php
@@ -287,6 +287,11 @@ class WC_REST_Products_V1_Controller extends WC_REST_Posts_Controller {
 		// Add gallery images.
 		$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );

+		if ( ! empty( $attachment_ids ) ) {
+			// Prime caches to reduce future queries.
+			_prime_post_caches( $attachment_ids );
+		}
+
 		// Build image data.
 		foreach ( $attachment_ids as $position => $attachment_id ) {
 			$attachment_post = get_post( $attachment_id );
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-products-v2-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-products-v2-controller.php
index 098ba998ca4..ebc70f0de0f 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-products-v2-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-products-v2-controller.php
@@ -530,6 +530,11 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller {
 		// Add gallery images.
 		$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );

+		if ( ! empty( $attachment_ids ) ) {
+			// Prime caches to reduce future queries.
+			_prime_post_caches( $attachment_ids );
+		}
+
 		// Build image data.
 		foreach ( $attachment_ids as $position => $attachment_id ) {
 			$attachment_post = get_post( $attachment_id );
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
index e07398bdca6..8941ec00ec1 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
@@ -174,6 +174,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
 		// Add gallery images.
 		$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );

+		if ( ! empty( $attachment_ids ) ) {
+			// Prime caches to reduce future queries.
+			_prime_post_caches( $attachment_ids );
+		}
+
 		// Build image data.
 		foreach ( $attachment_ids as $attachment_id ) {
 			$attachment_post = get_post( $attachment_id );