-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Default to querying posts when on singular content #65067
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
88fc422
Fix linter warnings
mikachan 094bddc
Replace the postType if current post type is different
mikachan 49ba8ca
Remove currentPostType logic
mikachan 51f9383
Default to posts if is_singular
mikachan 4053496
Update test_rendering_post_template_with_main_query_loop_already_star…
mikachan a2c1429
Add test for query loop not inside a singular query
mikachan 13bfbcc
Revert changes to QueryContent
mikachan cbc42d7
Merge branch 'trunk' into fix/query-loop-posttype
mikachan 58c0956
Show query type control only when on a template
mikachan e5f8195
Merge branch 'trunk' into fix/query-loop-posttype
mikachan 457f0f3
Move template logic to QueryInspectorControls
mikachan 92d937d
Ensure inherit value is updated when not in a template
mikachan 82a81c5
Update comment
mikachan ba494de
Rename showDefaultControl to isTemplate
mikachan 40ecda4
Get postType from context
mikachan 0f26b80
Add a check for singular content based on available post type
mikachan 52aaf5a
Move inherit reset to a useEffect
mikachan 47c84a7
Move isTemplate logic to QueryContent
mikachan 7447863
Fix lint warnings
mikachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { useEffect, useCallback } from '@wordpress/element'; | ||
import { | ||
BlockControls, | ||
InspectorControls, | ||
|
@@ -32,6 +32,7 @@ export default function QueryContent( { | |
openPatternSelectionModal, | ||
name, | ||
clientId, | ||
context, | ||
} ) { | ||
const { | ||
queryId, | ||
|
@@ -41,13 +42,24 @@ export default function QueryContent( { | |
tagName: TagName = 'div', | ||
query: { inherit } = {}, | ||
} = attributes; | ||
const { postType } = context; | ||
const { __unstableMarkNextChangeAsNotPersistent } = | ||
useDispatch( blockEditorStore ); | ||
const instanceId = useInstanceId( QueryContent ); | ||
const blockProps = useBlockProps(); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
template: TEMPLATE, | ||
} ); | ||
const isTemplate = useSelect( | ||
( select ) => { | ||
const currentTemplate = | ||
select( coreStore ).__experimentalGetTemplateForLink()?.type; | ||
const isInTemplate = 'wp_template' === currentTemplate; | ||
const isInSingularContent = postType !== undefined; | ||
return isInTemplate && ! isInSingularContent; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was |
||
}, | ||
[ postType ] | ||
); | ||
const { postsPerPage } = useSelect( ( select ) => { | ||
const { getSettings } = select( blockEditorStore ); | ||
const { getEntityRecord, getEntityRecordEdits, canUser } = | ||
|
@@ -81,6 +93,10 @@ export default function QueryContent( { | |
// Changes in query property (which is an object) need to be in the same callback, | ||
// because updates are batched after the render and changes in different query properties | ||
// would cause to override previous wanted changes. | ||
const updateQuery = useCallback( | ||
( newQuery ) => setAttributes( { query: { ...query, ...newQuery } } ), | ||
[ query, setAttributes ] | ||
); | ||
Comment on lines
+96
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was from fixing a lint warning. |
||
useEffect( () => { | ||
const newQuery = {}; | ||
// When we inherit from global query always need to set the `perPage` | ||
|
@@ -90,21 +106,37 @@ export default function QueryContent( { | |
} else if ( ! query.perPage && postsPerPage ) { | ||
newQuery.perPage = postsPerPage; | ||
} | ||
// We need to reset the `inherit` value if not in a template, as queries | ||
// are not inherited when outside a template (e.g. when in singular content). | ||
if ( ! isTemplate && query.inherit ) { | ||
newQuery.inherit = false; | ||
} | ||
if ( !! Object.keys( newQuery ).length ) { | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
updateQuery( newQuery ); | ||
} | ||
}, [ query.perPage, postsPerPage, inherit ] ); | ||
}, [ | ||
query.perPage, | ||
postsPerPage, | ||
inherit, | ||
isTemplate, | ||
query.inherit, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
updateQuery, | ||
] ); | ||
// We need this for multi-query block pagination. | ||
// Query parameters for each block are scoped to their ID. | ||
useEffect( () => { | ||
if ( ! Number.isFinite( queryId ) ) { | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
setAttributes( { queryId: instanceId } ); | ||
} | ||
}, [ queryId, instanceId ] ); | ||
const updateQuery = ( newQuery ) => | ||
setAttributes( { query: { ...query, ...newQuery } } ); | ||
}, [ | ||
queryId, | ||
instanceId, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
setAttributes, | ||
] ); | ||
const updateDisplayLayout = ( newDisplayLayout ) => | ||
setAttributes( { | ||
displayLayout: { ...displayLayout, ...newDisplayLayout }, | ||
|
@@ -135,6 +167,7 @@ export default function QueryContent( { | |
setDisplayLayout={ updateDisplayLayout } | ||
setAttributes={ setAttributes } | ||
clientId={ clientId } | ||
isTemplate={ isTemplate } | ||
/> | ||
</InspectorControls> | ||
<BlockControls> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid call, it's missing the
link
argument. Without this argument it's essentially checking wither we are in the site editor or not, because in the site editor this will always load the front page template, and in any other editor this will load nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining this. Just closing the loop here that this was fixed in #65820.