Skip to content

Commit

Permalink
query-loop block: Run the main query when queryId is not set (WordPre…
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 9, 2020
1 parent d0f883b commit 14eabe5
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 14eabe5

Please sign in to comment.