apply_filters( 'relevanssi_highlight_query', string $query )

This filter hook filters the search terms from the user before Relevanssi uses it to add highlights to the excerpts or post content.


(string) The query string from the user.

More information

When Relevanssi adds highlights to the post excerpt (or post content), Relevanssi takes the search query, tokenizes it and uses the tokens to highlight the words of the query string in the excerpt. This filter hook can be used to modify the query string, but only for this purpose. Changing the query string here doesn’t affect the search in any other way.

Something you can do here is to make the search query a phrase. This way, the search won’t be restricted by this, but Relevanssi will only highlight the whole search string and not the individual words. To achieve this, add this to your site:

add_filter( 'relevanssi_highlight_query', function( $query ) {
  if ( false === strpos( $query, '"' ) ) {
    $query = '"' . $query . '"';
  return $query;
} );