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

Support an accurate preview of the Related Posts block inside query loop block #2825

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

JakePT
Copy link
Contributor

@JakePT JakePT commented Jun 2, 2022

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

  1. With a block theme activated edit a template that uses the Query Loop block.
  2. Inside the Query Loop block, add the Related Posts block to the post template. The preview should show the correct related posts for each post in the loop.
  3. Edit an individual post and add the Related Posts block. The preview should show the correct related posts for that post.
  4. While editing that post, add a Query Loop block and add the Related Posts block to the post template. The preview should show the correct related posts for each post in the loop. The Related Posts block outside the loop should continue to show the related Posts for the post being edited.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

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

@JakePT JakePT requested a review from felipeelia June 2, 2022 21:28
@JakePT JakePT self-assigned this Jun 2, 2022
@JakePT JakePT added the module:related-posts Issues related to the Related Posts functionality label Jun 2, 2022
@felipeelia felipeelia added this to the 4.2.1 milestone Jun 7, 2022
@felipeelia felipeelia assigned felipeelia and unassigned JakePT Jun 7, 2022
@felipeelia
Copy link
Member

@JakePT testing with WordPress 5.8 and a regular theme, when I go to the Customizer and try to manage blocks/widgets there, I see two different sets of posts:

image
(Facets and the widget do not render anything on the homepage.) Is this difference between the previews something we can address?

@felipeelia felipeelia assigned JakePT and unassigned felipeelia Jun 20, 2022
@JakePT
Copy link
Contributor Author

JakePT commented Jun 21, 2022

@felipeelia I've double checked and this particular difference isn't a regression, but you're right that it's not addressed by this PR. The problem is that I don't believe there's any way for the block to 'know' the context of the widget area in the preview.

The block currently uses get_the_ID() when being rendered, and that will vary based on what page is being viewed and where the widget area is on that page. I think this gets at the issue raised in #2796 and whether we should show the block at all on archive pages. But even if we did only show it on single pages I don't know if the block can know which page is being previewed. I'll reach out on Slack to see if anyone has an idea.

@felipeelia felipeelia merged commit 5258aba into develop Jun 21, 2022
@felipeelia felipeelia deleted the feature/related-posts-postid-context branch June 21, 2022 14:22
felipeelia added a commit that referenced this pull request Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:related-posts Issues related to the Related Posts functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants