relevanssi_fallback

apply_filters( 'relevanssi_fallback', array $params )

This filter can be used to implement a fallback search with adjusted parameters.

Parameters

$params
(array) The parameter array. The query parameters are stored in $params['args'].

More information

When Relevanssi does a search that finds no results, a fallback may be triggered. Relevanssi has a built-in fallback search that can be controlled from the settings: after an AND search fails to find anything, Relevanssi can fall back and do an OR search. After that, this filter hook offers another chance to do a fallback search.

When you want to implement a fallback search, you take the search parameters from $params['args'], adjust them the way you want, then do a search with relevanssi_search() and put the return value from that function to $params['return']. Then you just return the $params array.

∞ warning! Remember to disable the fallback filter in before running relevanssi_search(), otherwise you risk ending up in an infinite loop!

add_filter( 'relevanssi_fallback', 'rlv_fallback_search' );
function rlv_fallback_search( $params ) {
  $args = $params['args'];
  
  // Modify the arguments somehow.
  
  remove_filter( 'relevanssi_fallback', 'rlv_fallback_search' );
  $params['return'] = relevanssi_search( $args );
  add_filter( 'relevanssi_fallback', 'rlv_fallback_search' );

  return $params;
}

Examples

For a real-life example of how this would work, you can take a look at this knowledge base entry about forcing a phrase search. Since the phrase search can fail to find results, there is also implemented a fallback search that disables the phrase search and tries without it.

You can also do a fallback search that automatically uses the “Did you mean” corrections to perform a search. For instructions of that, see this knowledge base entry.