Skip to content

Commit

Permalink
Prevent query block from looping in classic themes (#43221)
Browse files Browse the repository at this point in the history
* clone the global WP_Query inside the query loop block

* always reset post data after query loop
  • Loading branch information
draganescu authored and gziolo committed Aug 23, 2022
1 parent 493d14f commit 3c19b1b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -99,9 +99,12 @@ function render_block_core_post_template( $attributes, $content, $block ) {
$content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
}

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(
'<ul %1$s>%2$s</ul>',
Expand Down

0 comments on commit 3c19b1b

Please sign in to comment.