-
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
Prevent query block from looping in classic themes #43221
Conversation
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.
Cloning the WP_Query
class should be fine as long as none of the properties that are objects are modified (read more on this topic in Don't clone your php objects, DeepCopy them).
Otherwise the global query would be working with altered objects later on. That doesn't seem to be the case here since the query is used to loop through a list of posts and render them. Even though I would recommend running some tests where the query parameters include meta and taxonomy queries.
However, I think the post data should be reset using wp_reset_postdata
further down in code even when the global query is used.
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.
Suggested changes look good to me. As long as the block only loops through the posts (rather than changing the query properties that are objects), cloning is sufficient here.
9e2ec4f
to
78a0eb4
Compare
Removing the "Backport to Gutenberg RC" as this bug seems to have been introduced in April, not during the 13.9 release cycle. It can go in the next Gutenberg release. |
Merged w/ failing react native tests as to me they seemed unrelated 🙏🏻 |
* clone the global WP_Query inside the query loop block * always reset post data after query loop
Cherry-picked with 3c19b1b for WordPress 6.0.2 release. |
What?
Closes #43198
Why?
In classic themes templates depend on
have_posts()
to returnfalse
to exit the loop. The change added by #40656uses the
global WP_Query
to figure out global query wheninherit
ison
. This in turn loops over the global postsobject and resets it once it reaches the end. Thus when rendering posts and meeting one post using the query loop block the
upper most
have_posts
is never false (because the global posts always get reset).How?
An easy and quick fix is to
clone
the global WP_Query. This allows the block to render the loop without resetting theglobal posts. This way the block renders the correct loop but does not interfere with the global loop.
Testing Instructions
a) choose
b) then choose again in the modal
Screenshots or screencast
N/A