Skip to content

Commit

Permalink
[Block Library - Query Loop]: Fix broken preview in specific category…
Browse files Browse the repository at this point in the history
… template (#44294)
  • Loading branch information
ntsekouras authored and ockham committed Sep 26, 2022
1 parent 20de445 commit e1786b9
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions 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, useEntityRecords } from '@wordpress/core-data';
import { store as coreStore } from '@wordpress/core-data';

const TEMPLATE = [
[ 'core/post-title' ],
Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) {
// 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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -210,9 +204,6 @@ export default function PostTemplateEdit( {
parents,
restQueryArgs,
previewPostType,
categories,
categorySlug,
hasResolvedCategories,
]
);
const blockContexts = useMemo(
Expand Down

0 comments on commit e1786b9

Please sign in to comment.