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

Remove sticky posts setting when we inherit the query #40656

Merged
merged 10 commits into from
Jul 6, 2022
22 changes: 8 additions & 14 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,16 @@ function render_block_core_post_template( $attributes, $content, $block ) {
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];

$query_args = build_query_vars_from_query_block( $block, $page );
// Override the custom query with the global query if needed.
// Use 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_args['offset'] );
$query_args = wp_parse_args( $wp_query->query_vars, $query_args );

if ( empty( $query_args['post_type'] ) && is_singular() ) {
$query_args['post_type'] = get_post_type( get_the_ID() );
}
}
$query = $wp_query;
} else {
$query_args = build_query_vars_from_query_block( $block, $page );
$query = new WP_Query( $query_args );
}

$query = new WP_Query( $query_args );

if ( ! $query->have_posts() ) {
return '';
}
Expand Down Expand Up @@ -107,7 +99,9 @@ function render_block_core_post_template( $attributes, $content, $block ) {
$content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
}

wp_reset_postdata();
if ( ! $use_global_query ) {
wp_reset_postdata();
}

return sprintf(
'<ul %1$s>%2$s</ul>',
Expand Down
13 changes: 7 additions & 6 deletions packages/block-library/src/query-no-results/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ function render_block_core_query_no_results( $attributes, $content, $block ) {

$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
aristath marked this conversation as resolved.
Show resolved Hide resolved
$query_args = build_query_vars_from_query_block( $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 ) ) {
$query_args = wp_parse_args( $wp_query->query_vars, $query_args );
}
$query = $wp_query;
} else {
$query_args = build_query_vars_from_query_block( $block, $page );
$query = new WP_Query( $query_args );
}
$query = new WP_Query( $query_args );

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

wp_reset_postdata();
if ( ! $use_global_query ) {
wp_reset_postdata();
}

return sprintf(
'<div %1$s>%2$s</div>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default function QueryInspectorControls( {
onChange={ setQuery }
/>
) }
{ showSticky && (
{ ! inherit && showSticky && (
<StickyControl
value={ sticky }
onChange={ ( value ) => setQuery( { sticky: value } ) }
Expand Down