-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix query loop bugs by correctly relying on the main query and removing problematic workaround #49904
Conversation
… needs to be handled in block templates instead.
Flaky tests detected in 67f582a. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6003053661
|
Thank you, @felixarntz! I still have to start testing changes properly, but I wanted to leave a small note: We'll need to test previewing unsaved changes for posts/pages. |
On WooCommerce Blocks, we are working on the Blockified Single Product Template. We noticed a wrong behavior in the template when the |
Cross commenting here from the core PR that updates 2023:
That was my first thought too and it seems quite hard to reinforce such a restriction, since with the site editor you can edit visually a template and can start with an empty one. Also what happens to other existing block themes that do not have these updates? I'm all for removing the call to Calling From a different comment:
|
Cross-commenting my reply:
That's a great point. It suggests to me that we should probably find a low-level solution in which we ensure the loop is always properly started regardless of using the
This I find a confusing statement. Either we care about back compat, or we don't :) Not having the Maybe we can add some intelligent logic where we in block templates without a To conclude, I see the point of not putting this problem on block theme developers, so maybe this PR is not the right way to go. But the problem still needs to be fixed. Probably best to continue the conversation here and park the TT3 PR for now as some alternative approach that likely is not the desirable solution. |
Together with @gziolo, we took a deep dive into the problems here at WordCamp US, and we have since identified the root issue and solution. While the original PR was trying to tackle a different problem, we identified that that is not the key issue, so the PR description has been updated to outline in a detailed way what is happening and why. A core ticket for the root problem has been opened in https://core.trac.wordpress.org/ticket/59225. Please review the PR description carefully as quite some history research was needed to understand the problem, particularly as this PR addresses 3 different intertwined issues. The PR has been updated and is ready for full review and testing. Testing instructions can be found in the PR description. cc @ebinnion @draganescu @aristath @ntsekouras @Mamaduka since you were involved in the referenced issues |
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ phpunit/blocks/render-post-template-test.php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks as a good fix on top of the previous fixes, one that learned how to only solve the edgecase issues, without breaking expectations. We still clone the query but only doing it when it makes sense. When I authored #43221 I wasn't aware it would cause other problems - the extra check here is a welcome addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested thoroughly, but the code here looks good and makes sense to me. Thanks!
I'm going to test the changes with the patch prepared for WordPress Core. It's great to see that the Gutenberg part landed so quickly. Thank you all for help. |
@gziolo This can be backported now that https://core.trac.wordpress.org/changeset/56507 has been committed. I have already implemented core specific tests in WordPress/wordpress-develop#5103 (which is otherwise a mirror of the changes here). Can we add those tests together with the commit to port the changes or would that need to happen separately after porting over the changes? |
Has anyone tested post previews with these changes? |
I'm wondering if it is possible to update |
@gziolo |
I can confirm that this PR fixes a problem I was just about to open an issue for: I'm developing a block theme and one of the templates has 2 different Thank you @felixarntz !! |
@felixarntz, I meant that we should consider patching 6.3 because I doubt it's possible to fix the issue for WP 6.2 and 6.3 in the Gutenberg plugin. I commented on Trac, too. Aside: I really don't know why the plugin still declares support for 6.2 😅 |
@gziolo Do you mean patching with the fix for https://core.trac.wordpress.org/ticket/58154, or this one here (which is for https://core.trac.wordpress.org/ticket/59225)? Just to confirm, since those are two separate fixes and tickets. Or do you mean both? |
Too many tickets and PRs 😅
I was referring to this commit in core, as it'd be difficult to add the same changes in the Gutenberg plugin. However, this PR could be also a good candidate to backport to WP 6.3.2. I would say both would be fine to include. I hope it isn't too complex as I see that WordPress/wordpress-develop#5103 is still open and not merged to Again, https://core.trac.wordpress.org/changeset/56507 is what I was talking about. |
This PR will fix https://core.trac.wordpress.org/ticket/58027 and https://core.trac.wordpress.org/ticket/59225 once merged into WP core. The bug can be replicated with Gutenberg as well though.
What?
Currently, block themes do not handle "the loop" correctly. Since their existence, WordPress themes have been expected to have the main
WP_Query
object go through the loop of posts when rendering posts, which is commonly done with code such as the following:The above logic ensures the WordPress query loop is correctly handled, e.g. calling
in_the_loop()
will returntrue
as expected.In block themes, this doesn't happen correctly though, the loop is not started when rendering posts which breaks the long-standing expectation that is relied on in both WP core and the plugin ecosystem. This is because the
core/post-template
block, when configured to use the global query (via theinherit
attribute), clones the global query object rather than using it directly. This leads to the main query loop never being started since it is only started on the cloned instance.Before that problem was introduced even, based on an older version of the query block which at the time also did not trigger the loop correctly, the lack of proper loop handling was "addressed" with a workaround in the
core/post-content
(and latercore/post-featured-image
) block which effectively forces the main query loop to start if it was not started before. That workaround is not a proper fix and itself causes bugs, such as the one outlined in https://core.trac.wordpress.org/ticket/58027.Looking at the history led to the following findings:
core/post-content
block which is a hack to force the loop to start if it hasn't started yet.core/post-featured-image
block.Why?
WordPress has always expected the query loop to be used when rendering posts, even when a template is only expected to render a single post (such as the
single
orpage
templates). Classic themes therefore start the loop even on those templates, but it looks like this was missed in block themes, which has led to the aforementioned problems.Workarounds like enforcing the loop in certain blocks is not a proper solution for that and as mentioned above has led to its own set of problems.
How?
This PR ensures that the loop is properly handled in block themes, in a way that fixes the bug in https://core.trac.wordpress.org/ticket/58027 while also maintaining a fix for Automattic/wp-calypso#66270 (comment).
core/post-template
block is updated to actually use the main query directly which will fix the loop to work correctly with anyin_the_loop()
checks and address https://core.trac.wordpress.org/ticket/59225.core/post-template
block, if there is already a loop (which would only happen when using the block in a classic theme, or an even more edge-case scenario where the block is used nested within anothercore/post-template
block), the cloning of the main query is maintained, however with the addition of aWP_Query::rewind_posts()
call on the cloned instance, to ensure that, even if the loop on the main query was already started, the cloned instance will render all relevant posts. This is effectively a more appropriate fix to [Bug]: Query Loop block causes infinite loop with older themes and inherit template Automattic/wp-calypso#66270 / Query loop infinite loop when inheriting query from template #43198.core/post-content
andcore/post-featured-image
is removed as it's no longer necessary following the above fix, which effectively will fix the bug reported in https://core.trac.wordpress.org/ticket/58027.Testing Instructions
Before testing, add the following code snippet to your WP installation (could be added anywhere, in a plugin, or hacked into any core or theme functions file):
core/paragraph
blocks).core/post-content
andcore/post-featured-image
blocks.core/post-content
andcore/post-featured-image
blocks from within the post template (which should lead to the featured image placeholders and content disappearing from all posts displayed).core/featured-image
block is technically displayed (if any of the posts has a featured image, you should see that on top of the page), forcing the loop to start before it should. So then when the actual theme attempts to start the loop, it has already reached the end and therefore displays nothing.Now, apply this PR to Gutenberg (or the mirror PR WordPress/wordpress-develop#5103 to WP core), then test the above again. You don't need to create more posts of course, so you can start at step 2. You should see in steps 5 and 7 that the bugs have been fixed:
Last but not least, test the following to ensure the bug fixed in Automattic/wp-calypso#66270 / #43198 remains fixed (i.e. no PHP logic infinite loop):
core/query
block to it (use any of the patterns, such as "Grid"). Make sure the "Inherit query from template" toggle is enabled in the sidebar.