Network wide search may switch away from the site the search is performed on #2121
Description
Describe the bug
When retrieving the posts in a network-wide search, a switch_to_blog()
is called as part of the_post
hook, but restore_current_blog()
is not called immediately after that, it's called in the next the_post
hook if the next post is from a different site. This works OK in the loop, but some plugins (Yoast for example) may trigger the hook outside the loop. In that case WordPress will be left switched to the blog the first result came from, and if it's not the blog the search was performed on, unexpected thing happen.
I didn't go that deep to investigate what exactly Yoast is doing, but the hook is triggered when it generates it's output in the <head />
.
Steps to Reproduce
I'm sorry, but I can't share a public URL where the bug can be observed.
- Create multisite WordPress installation with at least two blogs.
- Install and activate Yoast and ElasticPress.
- Do a search on the main blog that will return a post from the second site as a first result.
- The site name in the header will be of the second blog, not the main one.
Expected behavior
The expected behaviour would be the site name not to change because of the returned results.
Environment information
- WordPress version: 5.6
- Plugins and version: Yoast 15.6.2
- Theme and version: custom theme, N/A
Additional context
My workaround was the following:
add_action( 'the_post', __NAMESPACE__ . '\\maybe_restore_current_blog', 11, 1 );
function maybe_restore_current_blog( $post ) {
if ( in_the_loop() ) {
return;
}
if ( is_multisite() && ! empty( $post->site_id ) ) {
restore_current_blog();
}
}
Maybe at the end of QueryIntegration::maybe_switch_to_blog()
a similar check should be made:
if ( ! in_the_loop() ) {
restore_current_blog();
}