Restore current blog when the_post triggers outside the loop. #2283
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.
This Pull Request is a "take over" of #2144.
Description of the Change
As described in #2121, in multisite environment and searching the whole network when
the_post
is triggered outside the loop, the current site is not restored if the first result is from another blog. To fix the issue I'm checking if theWP_Query
instance is in the loop and if not, callingrestore_current_blog()
. To do this properly I had to add second argument toElasticPress\Indexable\Post\QueryIntegration->maybe_switch_to_blog()
.Alternate Designs
In the project where I first discovered the issue I added another function to
the_post
to restore the current blog, but that is not necessary if the issue can be addressed at it's roots.Benefits
Fix the issue, described in #2121. The original PR content:
Possible Drawbacks
I can't find any.
Verification Process
I tested that the issue dissappears in my test setup and made sure current tests are passing.
Checklist:
Applicable Issues
Issue #2121.
Changelog Entry
Fix: Restore current blog when the_post triggers outside the loop in multisite environment and the whole network is searched if the first result is from another blog. Props @gonzomir