From 674b49eabfaab2a202dd83b31a82d7a236573cd7 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Mon, 15 Aug 2022 10:49:15 +0300 Subject: [PATCH 1/2] clone the global WP_Query inside the query loop block --- packages/block-library/src/post-template/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 284cf3f89673fe..d1d08f3d83ec9a 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -48,7 +48,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); if ( $use_global_query ) { global $wp_query; - $query = $wp_query; + $query = clone $wp_query; } else { $query_args = build_query_vars_from_query_block( $block, $page ); $query = new WP_Query( $query_args ); From 78a0eb4e33dc9b62cfb29580e95e0bb2aa21acd1 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Tue, 16 Aug 2022 15:56:30 +0300 Subject: [PATCH 2/2] always reset post data after query loop --- packages/block-library/src/post-template/index.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index d1d08f3d83ec9a..c12a0e68b8e248 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -99,9 +99,12 @@ function render_block_core_post_template( $attributes, $content, $block ) { $content .= '
  • ' . $block_content . '
  • '; } - if ( ! $use_global_query ) { - wp_reset_postdata(); - } + /* + * Use this function to restore the context of the template tags + * from a secondary query loop back to the main query loop. + * Since we use two custom loops, it's safest to always restore. + */ + wp_reset_postdata(); return sprintf( '',