add_filter() depending on search term

I’m modifying the search query:

add_filter('posts_join', 'myfunctionjoin' )
add_filter( 'posts_where','myfunctionwhere' )

These functions slow down the search response a bit , so I would like to “activate” them only if search term doesn’t match a specific condition. I’m able to achieve that by referencing to $_GET['s']:

if ($_GET["s"] != 'something'){
  add_filter('posts_join', 'myfunctionjoin' )
  add_filter( 'posts_where','myfunctionwhere' )
}

but I would feel more safe to use the WordPress ‘search term’. I did try:

if ($wp_query->query_vars['s'] != 'something'){ 
  add_filter('posts_join', 'myfunctionjoin' )
  add_filter( 'posts_where','myfunctionwhere' )
}

but no success… How do I put the WordPress ‘search term’ in a condition to activate the filters?

Solutions Collecting From Web of "add_filter() depending on search term"

The pre_get_posts action runs before the posts_join and posts_where filters.

So, pre_get_posts is a suitable hook for conditionally adding callbacks to the posts_join and posts_where filters:

function wpse249060_pre_get_posts( $query ) {
    // Make sure we're doing a search query and that the search
    // term is NOT 'something'
    if ( $query->is_search() && $query->query_vars['s'] !== 'something' ) {
        add_filter( 'posts_join', 'wpse249060_posts_join' );
        add_filter( 'posts_where','wpse249060_posts_where' );
    }
}
add_action( 'pre_get_posts', 'wpse249060_pre_get_posts' );

function wpse249060_posts_join( $join ) {
    //exit ( print_r( $join ) );
    return $join;
}

function wpse249060_posts_where( $where ) {
    //exit ( print_r( $where ) );
    return $where;
}