Changing the default wp_search_stopwords

I want to change the default stopwords in the wordpress search. I’m sure it’s easy but I can’t figure it out.

Per this https://developer.wordpress.org/reference/hooks/wp_search_stopwords/ the default stopwords are:

$words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
        'Comma-separated list of search stopwords in your language' ) );

So, I would think I could just add_filter it in like this:

add_filter( 'wp_search_stopwords', array('meh') );

But meh is still being applied to the search results.

I found this https://core.trac.wordpress.org/ticket/25587 which has the following at the end, but I don’t know what to do in the do whatever you want part:

function a_whatever() {
    remove_filter( 'wp_search_stopwords', __FUNCTION__ );
    // do whatever you want
}
add_filter( 'wp_search_stopwords', 'a_whatever' );

Thanks!

Solutions Collecting From Web of "Changing the default wp_search_stopwords"

You are close: you indeed need to define a filter, but you’ve not quite gotten the filter definition right:

function add_meh_stopword($stopwords) {
    $stopwords[] = 'meh'; // *add* "meh" to the list of stopwords
    // if instead you want to completely replace the list of stopwords
    // use $stopwords = array('meh');
    return $stopwords;
}
add_filter( 'wp_search_stopwords', 'add_meh_stopword');

The second argument to the add_filter function takes a “callable”, i.e. in this case a function name. That function then returns the modified list of stopwords.

Hope this helps!