diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 7ee8b4dd1b7055..251947582c920a 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -18,6 +18,10 @@ function render_block_core_post_content( $attributes, $content, $block ) { return ''; } + if ( ! in_the_loop() ) { + the_post(); + } + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); return ( diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 293ad681ff0f06..30bc6ca75de08d 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -18,24 +18,25 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); - $query = construct_wp_query_args( $block, $page ); + $query_args = construct_wp_query_args( $block, $page ); // Override the custom query with the global query if needed. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); if ( $use_global_query ) { global $wp_query; if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. - unset( $query['offset'] ); - $query = wp_parse_args( $wp_query->query_vars, $query ); + unset( $query_args['offset'] ); + $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); - if ( empty( $query['post_type'] ) && is_singular() ) { - $query['post_type'] = get_post_type( get_the_ID() ); + if ( empty( $query_args['post_type'] ) && is_singular() ) { + $query_args['post_type'] = get_post_type( get_the_ID() ); } } } - $posts = get_posts( $query ); - if ( empty( $posts ) ) { + $query = new WP_Query( $query_args ); + + if ( ! $query->have_posts() ) { return ''; } @@ -49,19 +50,22 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); $content = ''; - foreach ( $posts as $post ) { + while ( $query->have_posts() ) { + $query->the_post(); $block_content = ( new WP_Block( $block->parsed_block, array( - 'postType' => $post->post_type, - 'postId' => $post->ID, + 'postType' => get_post_type(), + 'postId' => get_the_ID(), ) ) )->render( array( 'dynamic' => false ) ); $content .= "
  • {$block_content}
  • "; } + wp_reset_postdata(); + return sprintf( '', $wrapper_attributes,