Skip to main contentSkip to footer

Relevanssi by default does not consider the search query a phrase, or value posts where the words in the search query appear together. Sometimes people do expect that, especially in AND searches. Relevanssi however does not have the information about the nearby words, so it’s not possible for Relevanssi to factor that in.

Relevanssi does support the sentence parameter in WordPress, which makes all searches phrase searches. It’s easy to enable:

add_filter( 'relevanssi_modify_wp_query', 'rlv_force_sentence' );
/**
 * Enables the `sentence` parameter in Releavnssi queries.
 *
 * @param WP_Query $query The query object.
 *
 * @return WP_Query The modified query object.
 */
function rlv_force_sentence( $query ) {
  $query->set( 'sentence', true );
  return $query;
}

This makes all queries automatically phrase searches. Of course, that has a huge drawback of causing large amount of searches that don’t match a phrase find nothing.

In Relevanssi, you can run a fallback search. If nothing is found, the fallback is triggered with the relevanssi_fallback filter hook, and it’s possible to adjust the search parameters to disable the sentence parameter then:

add_filter( 'relevanssi_fallback', 'rlv_phrase_fallback' );
/**
 * Triggers a fallback search when nothing is found.
 *
 * @param array $args The search arguments in the index 'args'.
 *
 * @return array The search arguments, with the results from
 * relevanssi_search() in 'return'.
 */
function rlv_phrase_fallback( $args ) {
  $args['args']['sentence'] = false;
    
  // Disable this filter to avoid an infinite loop.
  remove_filter( 'relevanssi_fallback', 'rlv_phrase_fallback' );
  
  // Run a search with the modified arguments.
  $return = relevanssi_search( $args['args'] );
  
  // Re-enable the filter.
  add_filter( 'relevanssi_fallback', 'rlv_phrase_fallback' );
    
  $args['return'] = $return;
  return $args;
}

Now all searches are first phrase searches, then if nothing is found the fallback is triggered, the sentence parameter is disabled and the search is run again with the same arguments.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Divi
Make Divi include all post types Divi search form module restricts the search to only posts and pages. To get…
Modal search form shortcode

…search restrictions as the regular searchform shortcode. Parameters that are not modal settings are passed to the search form. Search Search this site Search for: will restrict the search form to just products. Search Search this site Search for: will restrict the search form to posts, pages and news. Search…Relevanssi includes a modal search form shortcode that opens the search form in a dialog. The shortcode is relevanssi_modal_search. Search Search this site Search for: will display a “Search” button that opens the modal search form. Changing the labels Use the label parameter to change the text on the button:……Search this site Search for: will restrict the search form to categories 10, 14 and 17. Search Search this site Search for: will do a multisite search on blogs 1, 2 and 3. These parameters are added to the search form as hidden input fields. Adding dropdowns The modal shortcode…

Ultimate FAQs
Ultimate FAQs is a FAQ plugin that can create FAQs. It has a search, and enabling Relevanssi may break that…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed