-
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
[Block Library - Query Loop]: Fix broken preview in specific category template #44294
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import { | |
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { Spinner } from '@wordpress/components'; | ||
import { store as coreStore, useEntityRecords } from '@wordpress/core-data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
const TEMPLATE = [ | ||
[ 'core/post-title' ], | ||
|
@@ -101,19 +101,6 @@ export default function PostTemplateEdit( { | |
} ) { | ||
const [ { page } ] = queryContext; | ||
const [ activeBlockContextId, setActiveBlockContextId ] = useState(); | ||
|
||
let categorySlug = null; | ||
if ( templateSlug?.startsWith( 'category-' ) ) { | ||
categorySlug = templateSlug.replace( 'category-', '' ); | ||
} | ||
const { records: categories, hasResolved: hasResolvedCategories } = | ||
useEntityRecords( 'taxonomy', 'category', { | ||
context: 'view', | ||
per_page: -1, | ||
_fields: [ 'id' ], | ||
slug: categorySlug, | ||
} ); | ||
|
||
const { posts, blocks } = useSelect( | ||
( select ) => { | ||
const { getEntityRecords, getTaxonomies } = select( coreStore ); | ||
|
@@ -123,12 +110,22 @@ export default function PostTemplateEdit( { | |
per_page: -1, | ||
context: 'view', | ||
} ); | ||
const templateCategory = | ||
inherit && | ||
templateSlug?.startsWith( 'category-' ) && | ||
getEntityRecords( 'taxonomy', 'category', { | ||
context: 'view', | ||
per_page: 1, | ||
_fields: [ 'id' ], | ||
slug: templateSlug.replace( 'category-', '' ), | ||
} ); | ||
const query = { | ||
offset: perPage ? perPage * ( page - 1 ) + offset : 0, | ||
order, | ||
orderby: orderBy, | ||
}; | ||
if ( taxQuery ) { | ||
// There is no need to build the taxQuery if we inherit. | ||
if ( taxQuery && ! inherit ) { | ||
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. It is a general improvement and not related to this issue. Am I correct? 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. You're correct, it become more obvious to me after checking this issue. We could probably skip some other args if we 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. Makes sense. |
||
// We have to build the tax query for the REST API and use as | ||
// keys the taxonomies `rest_base` with the `term ids` as values. | ||
const builtTaxQuery = Object.entries( taxQuery ).reduce( | ||
|
@@ -174,11 +171,8 @@ export default function PostTemplateEdit( { | |
if ( templateSlug?.startsWith( 'archive-' ) ) { | ||
query.postType = templateSlug.replace( 'archive-', '' ); | ||
postType = query.postType; | ||
} else if ( !! categorySlug && hasResolvedCategories ) { | ||
query.taxQuery = { | ||
category: categories.map( ( { id } ) => id ), | ||
}; | ||
taxQuery = query.taxQuery; | ||
} else if ( templateCategory ) { | ||
query.categories = templateCategory[ 0 ]?.id; | ||
} | ||
} | ||
// When we preview Query Loop blocks we should prefer the current | ||
|
@@ -210,9 +204,6 @@ export default function PostTemplateEdit( { | |
parents, | ||
restQueryArgs, | ||
previewPostType, | ||
categories, | ||
categorySlug, | ||
hasResolvedCategories, | ||
] | ||
); | ||
const blockContexts = useMemo( | ||
|
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.
Nice 🚀