I am new to WP and I am currently managing a website that has a problem with the search engine. When I search for phrases or keywords like (London restaurants) for example it shows up all information it has for London and then all information is has for restaurants. Whan I really need is to limit the search results to posts containing only both words in title or content. website is http://www.suzannesfiles.com Can you help? my email is firstname.lastname@example.org Thanks in advance for cooperation!
It’s not really documented anywhere, I think, but a search built with a “sentence” parameter will do a search for a phrase.
There’s three search parameters:
s: the search terms.
sentence: flag to make it do a phrase search
exact: flag to make it only match whole titles/posts.
A normal search looks like this:
But if you did this:
Then you’re doing a phrase search.
In your search form, you can add a checkbox to enable this sort of behavior:
<input type="checkbox" name="sentence" value="1" />Phrase search
This will not limit it to posts with both of those words, but posts with the phrase “London restaurants” only (case is ignored). Might not be what you’re looking for, exactly, but it’s the best I can tell you without changing core code.
Otto42, thanks for the fast reply but I don’t think that adding this code actually made any difference. I added it and I still get random search results, check it out, when I type london restaurant no matter if I check the box or not. It is a bit different when I type london +restaurant but not quite there, plus I can’t expect people to always search like that. I would be really grateful if you can advise on this!
Thanks so much
The plus is not something you type, the http protocol handles that for you.
Actually, looking through the code, the search function already acts in the way you’re describing it. Searching for “london restaurant” would return results with *both* “london” and “restaurant” in either the title or the content. There’s no “OR” option, just an “AND”.
Looking at your site, you appear to be using some kind of advanced search plugin.. I don’t have any idea what that does, I am speaking of WordPress’ native search capabilities only. Using that plugin changes the way search works, so disable it and use the normal search function capabilities if you want my words to mean anything. If you still want to use that plugin, you need to talk with the plugin author or read documentation for it.
I believe it is not working correctly because when I search for London restaurant the first result is: London Garden Design which does not contain the word restaurant at all. Have a look for yourself. Do you know if there is other code or command I can give to restrict the search engine to show up only posts that contain all the words I type in the search bar.
Thank you for your help and sorry for asking too much questions but I really need to get this fixed soon.
Actually now when I disabled the plugin it totaly shows random results not even matching the words I type in the search bar. This is a big problem!
furthermore it doesn’t matter what I type it shows always the same result
This is the normal search result, without using your plugin:
As you can see, it works fine. All the returned posts contain both “london” and “restaurant”.
You don’t have any posts with the phrase “london restaurant”, but if you did, this would return them:
The problem is both the plugin and your search form which uses that plugin. It uses “s=advanced” to trigger the plugin, so when you disable it, but don’t fix your form, you get all the results with a search for “advanced” instead.
thanks, that’s so useful information. Do you know how can I fix the form then? Do I need to put sth instead of advances there? I’m come from a design background so I’m really not good with coding. Sorry if I ask stupid questions
Here’s your current form:
<form method="get" id="ctas_form" action="http://www.suzannesfiles.com"> <input type="hidden" name="ctas_avancedsearch" value="true" /> <div align="right"><input type="text" value="" name="ctas_searchquery" id="ctas_searchquery" /> <input type="submit" value="search" /> <input type="checkbox" name="sentence" value="1" /> Phrase search </div> <input type="hidden" name="ctas_cat_6" id="ctas_cat_6" value="on" /> ... bunch more of these removed... </form>
Change that whole thing to this:
<form method="get" id="ctas_form" action="http://www.suzannesfiles.com/"> <div align="right"><input type="text" value="" name="s" id="ctas_searchquery" /> <input type="submit" value="search" /> <input type="checkbox" name="sentence" value="1" /> Phrase search </div> </form>
And disable or remove the plugin. I would also suggest changing those “ctas” id’s and such, but you may have to change some style rules if you do that.
thank you so much for your help. I just showed this to my boss but she thinks that the search engine is not what she expected. Do you know of a technology or any way we can improve it further more because the way this engine works now is not really functional. For example if I want to search london restaurants it pulls up info about Megève and Biarritz because they have both words in the text mentioned somewhere, but the files are totaly irrelevent. We can not offer this search engine to customers if it’s not working properly. I would be really grateful if you can advise on this and I promisse to stop bothering you the moment we resolve this problem. Tell me what you think the best solution for this situation is and I’ll try to fix it even if I have to go and manually change or add text to each file.
I deeply appreciate your help!
No technology can make it magically able to understand human language.
I’ve replaced the search on my site with a Google search because I liked the results it gives better and I get paid for people clicking on the ads as well. But you may not want that. I dunno.
The short of it is that you can probably make it search however you want, but it’s still going to be limited to algorithmic searches. It’s not going to know that “Biarritz” is not a valid result for “london restaurant” because you have do both of those in the text.
so can I make it show just posts in category restaurants let’s say when I type restaurants in the search bar first before everything else? That may work because people may want to see restaurant category results related to london when they type london restaurants, hotel, spa or florist let’s say? I don’t know if you’ll understand what I mean – for example it recognized the word restaurant and it matches it up with the category of posts and show this results first. After them does just a normal search. The categories are divided by types of services and not by location so there is not risk of matching both London and restaurant as a category.
Well, that advanced search plugin did have category restrictions. Perhaps it has a way to add a category dropdown, so that they can just choose “restaurant” in the dropdown. Look through the documentation for that plugin.
- The topic ‘How to search phrases’ is closed to new replies.