From 14eabe552b0a6ec8dace932ee1bef943bd3475ed Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Mon, 9 Nov 2020 11:51:58 +0000 Subject: [PATCH 1/2] query-loop block: Run the main query when queryId is not set (#25377) --- .../block-library/src/query-loop/index.php | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 5acc340bd66e23..959eab209b86c8 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -67,19 +67,58 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } - $posts = get_posts( $query ); + if ( isset( $block->content['queryId']) ) { - $content = ''; - foreach ( $posts as $post ) { - $content .= ( + $posts = get_posts( $query ); + + $content=''; + foreach ( $posts as $post ) { + $content.= ( + new WP_Block( + $block->parsed_block, + array( + 'postType'=>$post->post_type, + 'postId' =>$post->ID, + ) + ) + )->render( array( 'dynamic'=>false ) ); + } + } else { + /** + * Not in context of a `core/query` block; assume this is the main query and perform the loop. + */ + $content = render_block_core_query_loop_main_query($attributes, $content, $block); + } + return $content; +} + +/** + * Renders the `core/query-loop` block for the main query on the server. + * + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. + * + * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks. + */ +function render_block_core_query_loop_main_query( $attributes, $content, $block ) { + if ( have_posts() ) { + $content = ''; + while ( have_posts() ) { + the_post(); + $post = get_post(); + $content .= ( new WP_Block( $block->parsed_block, array( 'postType' => $post->post_type, - 'postId' => $post->ID, + 'postId' => $post->ID, ) ) - )->render( array( 'dynamic' => false ) ); + )->render(array('dynamic' => false)); + } + } else { + $content = __( "No posts found." ); } return $content; } From f9322d14e6ea8e9db9630206432f93d118f9c002 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Mon, 9 Nov 2020 12:04:36 +0000 Subject: [PATCH 2/2] Correct spelling - context not content (#25377) --- packages/block-library/src/query-loop/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 959eab209b86c8..27596c0dc657cd 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -67,7 +67,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } - if ( isset( $block->content['queryId']) ) { + if ( isset( $block->context['queryId']) ) { $posts = get_posts( $query );