relevanssi_search_ok

apply_filters( 'relevanssi_search_ok', boolean $search_ok, WP_Query $query )

This filter hook controls whether Relevanssi can perform a search or not.

Parameters

$search_ok
(boolean) If true, Relevanssi can run. If false, Relevanssi won’t run.

$query
(WP_Query) The query object.

More information

Relevanssi triggers on the posts_pre_query filter hook and runs the relevanssi_query() function. This function tries to figure out if the query is something where Relevanssi should be involved. If the query seems fine, Relevanssi will then run relevanssi_do_query() on the query object to replace the found posts with Relevanssi results.

Within this process, Relevanssi makes the following checks:

  • Does $query->is_search() return true?
  • Does $query->is_main_query() return true?
  • If $query->is_search() is true and $query->is_admin is true, is Relevanssi admin search enabled?
  • If $query->is_admin is true, is $query->query_vars['s'] not empty?
  • Does $query->get( 'relevanssi' ) return true?

If these check out, Relevanssi assumes this is a query that it can take over. Relevanssi then filters the boolean value with this filter hook. You can control the Relevanssi activation with this filter hook, for example, to block Relevanssi from specific queries.

Relevanssi also uses relevanssi_prevent_default_request to block the default request from running. If you want to disable Relevanssi from running, you need also to use that filter hook to make sure the Relevanssi doesn’t stop the default request.

Here are some examples:

One use for this filter hook is to stop Relevanssi from running termless searches. It is done like this:

add_filter(
  'relevanssi_search_ok',
  function( $ok, $query ) {
    return empty( $query->query_vars['s'] ) ? false : $ok;
  },
  10,
  2
);