diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 284cf3f89673fe..c12a0e68b8e248 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 ); @@ -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( '',