Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Query Loop: Don't overwrite the 'query.inherit' attribute value (#69698)
* Query Loop: Don't overwrite the 'query.inherit' attribute value
* A misc adjustments
* Display warning next to 'Query Type' control

Unlinked contributors: genemma, cheestudio.

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: luminuu <[email protected]>
Co-authored-by: jeflopodev <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: rinkalpagdar <[email protected]>
  • Loading branch information
7 people authored and t-hamano committed Jun 30, 2025
commit e8a0af6beecafe6e7e4b5ece7134f58bf7f1efcd
84 changes: 50 additions & 34 deletions packages/block-library/src/query/edit/inspector-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
TextControl,
SelectControl,
RangeControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Notice,
__experimentalVStack as VStack,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -113,8 +114,7 @@ export default function QueryInspectorControls( props ) {
}, [ querySearch, onChangeDebounced ] );

const orderByOptions = useOrderByOptions( postType );
const showInheritControl =
! isSingular && isControlAllowed( allowedControls, 'inherit' );
const showInheritControl = isControlAllowed( allowedControls, 'inherit' );
const showPostTypeControl =
! inherit && isControlAllowed( allowedControls, 'postType' );
const postTypeControlLabel = __( 'Post type' );
Expand Down Expand Up @@ -185,6 +185,10 @@ export default function QueryInspectorControls( props ) {
const showDisplayPanel =
showPostCountControl || showOffSetControl || showPagesControl;

// The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank).
// Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes.
const hasInheritanceWarning = isSingular && inherit;

return (
<>
{ showSettingsPanel && (
Expand All @@ -208,36 +212,48 @@ export default function QueryInspectorControls( props ) {
onDeselect={ () => setQuery( { inherit: true } ) }
isShownByDefault
>
<ToggleGroupControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Query type' ) }
isBlock
onChange={ ( value ) => {
setQuery( {
inherit: value === 'default',
} );
} }
help={
inherit
? __(
'Display a list of posts or custom post types based on the current template.'
)
: __(
'Display a list of posts or custom post types based on specific criteria.'
)
}
value={ !! inherit ? 'default' : 'custom' }
>
<ToggleGroupControlOption
value="default"
label={ __( 'Default' ) }
/>
<ToggleGroupControlOption
value="custom"
label={ __( 'Custom' ) }
/>
</ToggleGroupControl>
<VStack spacing={ 4 }>
<ToggleGroupControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Query type' ) }
isBlock
onChange={ ( value ) => {
setQuery( {
inherit: value === 'default',
} );
} }
help={
inherit
? __(
'Display a list of posts or custom post types based on the current template.'
)
: __(
'Display a list of posts or custom post types based on specific criteria.'
)
}
value={ !! inherit ? 'default' : 'custom' }
>
<ToggleGroupControlOption
value="default"
label={ __( 'Default' ) }
/>
<ToggleGroupControlOption
value="custom"
label={ __( 'Custom' ) }
/>
</ToggleGroupControl>
{ hasInheritanceWarning && (
<Notice
status="warning"
isDismissible={ false }
>
{ __(
'Cannot inherit the current template query when placed inside the singular content (e.g., post, page, 404, blank).'
) }
</Notice>
) }
</VStack>
</ToolsPanelItem>
) }

Expand Down
10 changes: 2 additions & 8 deletions packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,15 @@ export default function QueryContent( {
} else if ( ! query.perPage && postsPerPage ) {
newQuery.perPage = postsPerPage;
}
// We need to reset the `inherit` value if in a singular template, as queries
// are not inherited when in singular content (e.g. post, page, 404, blank).
if ( isSingular && query.inherit ) {
newQuery.inherit = false;
}

if ( !! Object.keys( newQuery ).length ) {
__unstableMarkNextChangeAsNotPersistent();
updateQuery( newQuery );
}
}, [
query.perPage,
query.inherit,
postsPerPage,
inherit,
isSingular,
postsPerPage,
__unstableMarkNextChangeAsNotPersistent,
updateQuery,
] );
Expand Down