Skip to content
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: It doesn't show sticky posts at the top when the query type is the default in the editor. #68595

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from 1 commit
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
54 changes: 44 additions & 10 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,6 @@ export default function PostTemplateEdit( {
query.format = format;
}

// If sticky is not set, it will return all posts in the results.
// If sticky is set to `only`, it will limit the results to sticky posts only.
// If it is anything else, it will exclude sticky posts from results. For the record the value stored is `exclude`.
if ( sticky ) {
query.sticky = sticky === 'only';
}
// If `inherit` is truthy, adjust conditionally the query to create a better preview.
if ( inherit ) {
// Change the post-type if needed.
Expand All @@ -203,14 +197,54 @@ export default function PostTemplateEdit( {
);
}
}

// When we preview Query Loop blocks we should prefer the current
// block's postType, which is passed through block context.
const usedPostType = previewPostType || postType;

// If sticky is not set, it will return all posts in the results.
// If sticky is set to `only`, it will limit the results to sticky posts only.
// If it is anything else, it will exclude sticky posts from results. For the record the value stored is `exclude`.
if ( sticky === 'only' ) {
Copy link
Contributor

@carolinan carolinan Jan 22, 2025

Choose a reason for hiding this comment

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

Perhaps these two conditions could be combined into one, where the value of sticky depends on if it is only/exclude?
It depends on what everyone prefers in terms of readability, but also on how we want to manage the introduction of ignore, #66222, which relies on this issue.

return {
posts: getEntityRecords( 'postType', usedPostType, {
...query,
...restQueryArgs,
sticky: true,
} ),
blocks: getBlocks( clientId ),
};
} else if ( sticky === 'exclude' ) {
return {
posts: getEntityRecords( 'postType', usedPostType, {
...query,
...restQueryArgs,
sticky: false,
} ),
blocks: getBlocks( clientId ),
};
}

const stickyPosts = getEntityRecords( 'postType', usedPostType, {
...query,
sticky: true,
per_page: -1,
} );

const regularPosts = getEntityRecords( 'postType', usedPostType, {
...query,
...restQueryArgs,
sticky: false,
per_page: perPage
? Math.max( 0, perPage - ( stickyPosts?.length || 0 ) )
: undefined,
} );

return {
posts: getEntityRecords( 'postType', usedPostType, {
...query,
...restQueryArgs,
} ),
posts:
stickyPosts && regularPosts
? [ ...stickyPosts, ...regularPosts ]
: null,
blocks: getBlocks( clientId ),
};
},
Expand Down
Loading