Search word gets blanked by mfn_search(), which breaks Yoast search redirects (29.0.5 and 29.0.7)
Hi,
I ran into a search problem on one of my sites and tracked it back to mfn_search() in functions/theme-functions.php. I know this came up before (discussion 35333, back in 2018), but it is now breaking search for my visitors, so I wanted to ask again.
When the theme's search finds something, it runs this (lines 3323-3325 in 29.0.5, 3294-3296 in 29.0.7):
$search_query->set('s', false);
$search_query->set('post__in', array_unique($post_ids));
$search_query->set('orderby', 'post__in');
After that, get_search_query() comes back empty for any plugin that asks for it. Your own templates are fine because they read $_GET['s'], but plugins that use the WordPress function get nothing.
Here is what that does on my site with Yoast SEO 28.7:
/search/nurse/ gets redirected to /?s, so the visitor lands on an empty search. That is Yoast's "Redirect pretty URLs to 'raw' formats" setting, which builds the address from get_search_query(). A word with no matches is fine: /search/zzqxvkkw/ goes to /?s=zzqxvkkw.
/?s=nurse was doing the same thing through Yoast's "Remove unregistered URL parameters" setting, until I added s to Yoast's allowed parameters.
The page title comes out as "You searched for | Site name", with the word missing.
To make sure it wasn't Yoast on its own, I ran the search from WP-CLI with your hook and then without it (remove_action on pre_get_posts). With the hook, searching for "nurse" leaves 's' set to false. Without it, 's' is still 'nurse'. A made-up word that matches nothing keeps its value either way. I didn't change any theme files.
One more thing I noticed: the ID queries in mfn_search() don't check post_status, so a word that only matches a draft or an old revision gets blanked too, and then the visitor sees no results at all.
The site is on 29.0.5 with WordPress 6.9.12 and PHP 8.3. I checked 29.0.7 as well. The function is the same there, and I got the same result on a 29.0.7 test install.
So my question: is clearing 's' actually needed for the builder search, or is it only there to stop WordPress adding its own LIKE search on top of post__in? If it's the second, could the term be left in place and the default search SQL dropped with the posts_search filter instead? I haven't tested that, so it's a question and not a patch. mfn_search_exact_word would need a look too, since it rebuilds the clause from 's'.
Happy to test a build if that helps.
Thanks!
Comments
Hi,
Thanks for letting us know and for the prompt message.
It will be corrected in the next update.
Best regards