Search word gets blanked by mfn_search(), which breaks Yoast search redirects (29.0.5 and 29.0.7)

edited 4:20AM in Theme support

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!

While waiting for a response from one of our team members, we recommend to check Support Center where it is highly likely that you will find the answer to your question in no time.

Sign In or Register to comment.
This website uses cookies

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you’ve provided to them or that they’ve collected from your use of their services.

Cookies are small text files that can be used by websites to make a user's experience more efficient.

The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This means that cookies which are categorized as necessary, are processed based on GDPR Art. 6 (1) (f). All other cookies, meaning those from the categories preferences and marketing, are processed based on GDPR Art. 6 (1) (a) GDPR.

This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages.

You can at any time change or withdraw your consent from the Cookie Declaration on our website.

Learn more about who we are, how you can contact us and how we process personal data in our Privacy Policy.

Please state your consent ID and date when you contact us regarding your consent.