Support an accurate preview of the Related Posts block inside query loop block #2825
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
The Related Posts block currently uses the post ID returned by
select('core/editor').getCurrentPostId()
to get the list of related posts for the preview. When adding the block to a post this works as expected and it displays the related posts for the post being edited, while adding the block to a widget area, where there is no ID available, it displays a generic list of posts as a preview.Similarly, when adding the block to a FSE template there is no ID available, so the generic list is used. However, this is also the case when using the block inside the Query Loop block, where there is an actual post that could theoretically be used. This is because
select('core/editor').getCurrentPostId()
only returns the ID for the post being edited, and there is no post when editing an FSE template. This also means that if the query loop block is used on a single post then the list of related posts for the edited post will be used, rather than those for each individual post in the loop or the generic list.This PR updates the Related Posts block to use the
postID
block context, which allows it to use the post ID of each post in the query loop block. The correct post ID is also returned by this context when editing single posts, or the template for a single post, so it can replace the method for getting the relevant post ID entirely.Possible Drawbacks
I'm not actually sure which version of WordPress core block contexts were introduced in, so this may require a higher minimum version.
Verification Process
Checklist:
Changelog Entry
Fixed - An issue where the Related Posts block would display the wrong posts in the preview when added inside a Query Loop block.
Credits
Props @JakePT