diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 461ba164dcc..06a339b779f 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -93,12 +93,11 @@ public function get_block_template_fallback( $template, $id, $template_type ) { } $wp_query_args = array( - 'post_name__in' => array( 'archive-product' ), - 'post_type' => $template_type, - 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), - 'posts_per_page' => 1, - 'no_found_rows' => true, - 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query + 'post_name__in' => array( 'archive-product', $slug ), + 'post_type' => $template_type, + 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), + 'no_found_rows' => true, + 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query array( 'taxonomy' => 'wp_theme', 'field' => 'name', @@ -109,6 +108,12 @@ public function get_block_template_fallback( $template, $id, $template_type ) { $template_query = new \WP_Query( $wp_query_args ); $posts = $template_query->posts; + // If we have more than one result from the query, it means that the current template is present in the db (has + // been customized by the user) and we should not return the `archive-product` template. + if ( count( $posts ) > 1 ) { + return null; + } + if ( count( $posts ) > 0 ) { $template = _build_block_template_result_from_post( $posts[0] ); @@ -117,6 +122,7 @@ public function get_block_template_fallback( $template, $id, $template_type ) { $template->slug = $slug; $template->title = BlockTemplateUtils::get_block_template_title( $slug ); $template->description = BlockTemplateUtils::get_block_template_description( $slug ); + unset( $template->source ); return $template; }