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

Block Library - Query: Use a WordPress loop for the query block #30405

Merged
merged 6 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
24 changes: 14 additions & 10 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
aristath marked this conversation as resolved.
Show resolved Hide resolved

if ( ! $query->have_posts() ) {
return '';
}

Expand All @@ -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 .= "<li>{$block_content}</li>";
}

wp_reset_postdata();
aristath marked this conversation as resolved.
Show resolved Hide resolved

return sprintf(
'<ul %1$s>%2$s</ul>',
$wrapper_attributes,
Expand Down