-
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
+122
−17
Merged
Changes from 2 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
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, | ||
|
@@ -46,10 +46,14 @@ export default function QueryContent( { | |
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
template: TEMPLATE, | ||
} ); | ||
const { postsPerPage } = useSelect( ( select ) => { | ||
const { postsPerPage, currentPostType } = useSelect( ( select ) => { | ||
const { getSettings } = select( blockEditorStore ); | ||
const { getEntityRecord, getEntityRecordEdits, canUser } = | ||
select( coreStore ); | ||
// @wordpress/block-library should not depend on @wordpress/editor. | ||
// However, we need to get the current post type in order to inherit the correct query. | ||
// eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
const { getCurrentPostType } = select( 'core/editor' ); | ||
const settingPerPage = canUser( 'read', { | ||
kind: 'root', | ||
name: 'site', | ||
|
@@ -68,6 +72,7 @@ export default function QueryContent( { | |
editedSettingPerPage || | ||
settingPerPage || | ||
DEFAULTS_POSTS_PER_PAGE, | ||
currentPostType: getCurrentPostType(), | ||
}; | ||
}, [] ); | ||
// There are some effects running where some initialization logic is | ||
|
@@ -76,6 +81,11 @@ export default function QueryContent( { | |
// resetting again, so we need to mark these changes as not persistent | ||
// with `__unstableMarkNextChangeAsNotPersistent`. | ||
|
||
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. |
||
|
||
// 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. | ||
|
@@ -92,17 +102,38 @@ export default function QueryContent( { | |
__unstableMarkNextChangeAsNotPersistent(); | ||
updateQuery( newQuery ); | ||
} | ||
}, [ query.perPage, postsPerPage, inherit ] ); | ||
// When we inherit from global query we should check if the current post type | ||
// is different from the query post type, excluding templates and patterns. | ||
// We should default to posts if the current post type is not valid. | ||
let newPostType = 'post'; | ||
const excludedPostTypes = [ 'wp_template', 'wp_block' ]; | ||
if ( ! excludedPostTypes.includes( currentPostType ) ) { | ||
newPostType = currentPostType; | ||
} | ||
if ( inherit && newPostType !== query.postType ) { | ||
query.postType = newPostType; | ||
} | ||
}, [ | ||
query, | ||
postsPerPage, | ||
inherit, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
updateQuery, | ||
currentPostType, | ||
] ); | ||
// 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 }, | ||
|
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.
If we need to access the current postType we should be able to get it via the block context by setting
usesContext
to includepostType
.However I'm not sure this approach is solving the problem described 🤔
Instead of showing the posts of the current post type I do think the default state for the inherit option should still be that is always renders posts of the posts posts type.
The issue is that today it doesn't do that when the query loop is inserted inside content rather than inside a template.
Rather than this I think it might be a better route to add a check in PHP to check whether something like whether the inherit option is set to true and the
is_singular
check resolves to true.In that case we change the default params to set the post type to
post
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 @fabiankaegy!
Ah thank you, that's helpful.
I agree - I was focused on making the Editor and the front end consistent, and assumed the front end was correct. But now I think it makes more sense for the query loop to default to querying posts, as this seems to be the safest assumption for most users. I've tried out
is_singular()
here: 51f9383This new behavior also matches the block description: