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

Ensure Query Loop Block queryId Uniqueness #58804

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- Query Loop Block: Ensure that the `queryId` attribute is always unique.

## 8.27.0 (2024-01-24)

## 8.26.0 (2024-01-10)
Expand Down
19 changes: 11 additions & 8 deletions packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function QueryContent( {
clientId,
} ) {
const {
queryId,
query,
displayLayout,
tagName: TagName = 'div',
Expand Down Expand Up @@ -79,14 +78,18 @@ export default function QueryContent( {
updateQuery( newQuery );
}
}, [ query.perPage, postsPerPage, inherit ] );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.

// Each instance of the block needs to have a unique queryId so that pagination works
// when multiple instances of this block are present. `instanceId` is the index of
// the block on the page and is stable across saves with automatic handling for
// re-indexing when these blocks are added, removed, or moved. Depending on
// what it's set to it might be changed during block editing but that won't
// negatively impact the functionality and pagination will still work.
useEffect( () => {
if ( ! Number.isFinite( queryId ) ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { queryId: instanceId } );
}
}, [ queryId, instanceId ] );
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { queryId: instanceId } );
}, [ instanceId ] );

const updateQuery = ( newQuery ) =>
setAttributes( { query: { ...query, ...newQuery } } );
const updateDisplayLayout = ( newDisplayLayout ) =>
Expand Down
Loading