SearchWP Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP’s many hooks

Customize Stopwords Per Engine

SearchWP uses stopwords (common words that are not helpful when searching, but can bloat the index) to omit words from its index and therefore ignoring those terms when searching as well. Removing stopwords increases relevance and improves performance.

By default SearchWP’s stopwords are global, applying to all Engines. If you have a situation where you would like to customize stopwords per Engine we can take advantage of SearchWP’s hooks to do just that.

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
/**
* Customize SearchWP stopwords per Engine.
*/
// Optional: remove all Stopwords so you can add only unique Stopwords per Engine.
add_filter( 'searchwp\stopwords', '__return_empty_array' );
// Add unique stopword(s) for a single SearchWP Engine.
add_filter( 'searchwp\query\search_string', function( $search_string, $query ) {
// Remove "apple" and "orange" for my_engine searches.
if ( 'my_engine' === $query->get_engine() ) {
$search_string = str_replace( [ 'apple', 'orange' ], '', $search_string );
}
return $search_string;
}, 20, 2 );

In this snippet we are first using the searchwp\stopwords hook to remove all of SearchWP’s stopwords. We need to work from a blank slate, indexing all content. If we were to store stopwords on the Settings tab of the SearchWP settings screen, those stopwords would be ignored before we had a chance to customize them per Engine.

The second hook searchwp\query\search_string applies the logic of stopwords on every search submitted to SearchWP.

In the above snippet we are removing apple and orange from the search string only if the Engine in use is my_engine.

apple and orange are not removed if any other Engine is used.

This snippet can be customized for any Engine(s) and any stopwords.

A Better WordPress Search Experience

Get the best WordPress search plugin. Trusted by 50,000+ websites.

Get SearchWP Now
Multiple Search Engines Icon