Custom Stop Words for Site Search
Ignores filler words such as "can", "my" or "need" in multi-word searches, so "how can I reset my password" finds posts about resetting passwords.
Opens WPIDE on
Code
// Extra words to ignore in multi-word searches, so "how can I reset my password" searches for "reset password".
$wpide_extra_stopwords = array( 'can', 'do', 'does', 'i', 'my', 'me', 'you', 'your', 'want', 'need', 'please', 'why', 'which', 'get' );
// Words WordPress ignores by default that matter on your site (for example 'who' for a band site).
$wpide_keep_words = array();
add_filter( 'wp_search_stopwords', function ( $stopwords ) use ( $wpide_extra_stopwords, $wpide_keep_words ) {
$stopwords = array_merge( $stopwords, array_map( 'strtolower', $wpide_extra_stopwords ) );
return array_values( array_diff( array_unique( $stopwords ), array_map( 'strtolower', $wpide_keep_words ) ) );
} );
Notes
WordPress only drops stop words from searches with several words; list words that matter on your site under the keep list.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.