Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query-loop block: Run the main query when queryId is not set (#25377) #26825

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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->context['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