Skip to content

Commit

Permalink
Remove sticky posts setting when we inherit the query (#40656)
Browse files Browse the repository at this point in the history
* reset the sticky posts query var for inherited query

* merge query vars into query defaults and unset empty s query var

* unser all query block defaults which are not editable when inherit is true

* fixes linting PHP problems

* uses global query as is and hides and UI that overrides it

* removes mistaken changes

* removes query overriding on inherit for no results block, although why?

* Update packages/block-library/src/query-no-results/index.php

* Adds co-authors

Co-authored-by: Jonny Harris <237508+spacedmonkey@users.noreply.github.com>
Co-authored-by: Huub <50170696+huubl@users.noreply.github.com>

* Update packages/block-library/src/query-no-results/index.php

CS alignments fix

Co-authored-by: Jonny Harris <237508+spacedmonkey@users.noreply.github.com>
Co-authored-by: Huub <50170696+huubl@users.noreply.github.com>
Co-authored-by: Ari Stathopoulos <aristath@gmail.com>
  • Loading branch information
4 people authored and gziolo committed Aug 23, 2022
1 parent fdce171 commit 493d14f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
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
18 changes: 10 additions & 8 deletions packages/block-library/src/query-no-results/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ function render_block_core_query_no_results( $attributes, $content, $block ) {
return '';
}

$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 );
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];

// 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 @@ -130,7 +130,7 @@ export default function QueryInspectorControls( {
onChange={ setQuery }
/>
) }
{ showSticky && (
{ ! inherit && showSticky && (
<StickyControl
value={ sticky }
onChange={ ( value ) => setQuery( { sticky: value } ) }
Expand Down

0 comments on commit 493d14f

Please sign in to comment.