Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.