As of version 3.1, SearchWP supports quoted searches, sometimes called phrase or sentence-based searches. This allows your visitors to add quotes to their searches so as to force exact matches for that quoted string.
For example searching for
coffee mugs vs.
To enable this functionality, tick the appropriate checkbox on the Advanced tab of the SearchWP settings screen:
When enabled, SearchWP will check search queries for terms wrapped in quotes, and attempt to find exact matches for the quoted string. Referencing again the earlier example of
coffee mugs vs.
- In following how SearchWP works SearchWP will first perform a search using
ANDlogic, ensuring that all search terms are present. If no results are found in that pass, SearchWP will automatically revert to
ORlogic. This behavior can be customized using hooks.
- Because the search string is wrapped in quotes, SearchWP will attempt to find results that have an exact match of
coffee mugs(note caveats) when performing a search. If no results are found that have exact matches, the quotes will be removed and search performed again following that pattern.
By nature, quoted searches restrict/refine search results. This can result in no results being returned. With SearchWP, if no results are found with a quoted search, the algorithm will fall back to performing the search without quotes. A notice is automatically output to indicate the change.
⚠️ Style (CSS) customization is likely required ⚠️
If you would like to prevent the automatic output of this notice you can use the
searchwp_auto_output_revised_search_query hook to disable it. Subsequently you can use the
searchwp_revised_search_query to implement your own in place of the automatic output.
The above screenshot outlines the automatic output in Twenty Nineteen, a default WordPress theme. The following CSS can be added to improve its appearance:
Note: (You can add Custom CSS to your site by navigating to Appearance > Customize > Additional CSS in your WordPress Dashboard)
|margin: 0 calc(10% + 60px) 1em;|
|border: 1px solid #e7e7e7;|
Which results in a notification that better matches the site design:
There are some limitations to consider when evaluating SearchWP’s exact match implementation. SearchWP is a token-based search system so by nature SearchWP’s index does not contain full details about exact matches throughout its search index. SearchWP’s indexer also allows for content customization during indexing, which can further obscure exact matches in some cases.
As a result, SearchWP is not able to find exact matches in the following cases:
- Shortcode-generated content
- Extra metadata (although there is a workaround)
- Custom database table content
- Content modified during indexing
What this means is that SearchWP’s quoted search feature matches content stored in native post attributes (e.g. Title, content, etc.), Custom Fields (post metadata) in most cases, and taxonomy terms.
Because of these limitations, SearchWP’s quoted search support is an opt-in feature, requiring you to enable the functionality either by ticking the checkbox on the Advanced tab of the SearchWP settings screen, or using the
There are some limited ways to work around the limitations of SearchWP’s quoted search support, as outlined below.
Extra Metadata Workaround
When SearchWP indexes Extra metadata the data is stored only in SearchWP’s index, which means the only record of the data is one that has been tokenized by the indexer.
In order to avoid the issue of SearchWP not being able to de-tokenize the content to perform exact match queries, there is a hook you can use to have SearchWP persist your Extra Metadata as actual records in your
This is an opt-in behavior, as SearchWP stores Extra Metadata only in its index to keep database usage at a minimum. When using
searchwp_persist_extra_metadata SearchWP will take a few steps to make it possible to use Extra Metadata to perform quoted searches:
- In addition to the SearchWP index, your Extra Metadata will be stored in your
postmetadatabase table as well
- You will still need to add your Extra Metadata keys as per the docs (this is important!)
- Persisted Extra Metadata will not be indexed twice, it will remain as Extra Metadata as far as the SearchWP index is concerned
- Your Custom Field dropdown keys will exclude these internal storage keys so they won’t be directly available
- When evaluating quoted searches, the Extra Metadata keys that have been added to your engine will be utilized to perform matches against the persisted metadata that now appears in your