Skip to content
Merged
Changes from all commits
Commits
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
22 changes: 21 additions & 1 deletion packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Spinner } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as coreStore, useEntityRecords } from '@wordpress/core-data';

const TEMPLATE = [
[ 'core/post-title' ],
Expand Down Expand Up @@ -95,6 +95,18 @@ 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 );
Expand Down Expand Up @@ -155,6 +167,11 @@ export default function PostTemplateEdit( {
if ( templateSlug?.startsWith( 'archive-' ) ) {
query.postType = templateSlug.replace( 'archive-', '' );
postType = query.postType;
} else if ( !! categorySlug && hasResolvedCategories ) {
query.taxQuery = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful, though we need to make it so any taxonomy works by default. I'm thinking on tags but also any 3rd party taxonomies (e.g.: product categories that Woo uses, etc).

category: categories.map( ( { id } ) => id ),
};
taxQuery = query.taxQuery;
}
}
// When we preview Query Loop blocks we should prefer the current
Expand Down Expand Up @@ -182,6 +199,9 @@ export default function PostTemplateEdit( {
taxQuery,
parents,
previewPostType,
categories,
categorySlug,
hasResolvedCategories,
]
);
const blockContexts = useMemo(
Expand Down