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()
returntrue
? - Does
$query->is_main_query()
returntrue
? - If
$query->is_search()
istrue
and$query->is_admin
istrue
, is Relevanssi admin search enabled? - If
$query->is_admin
istrue
, is$query->query_vars['s']
not empty? - Does
$query->get( 'relevanssi' )
returntrue
?
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:
- Preventing Relevanssi from searching Elementor Library
- Preventing Relevanssi from searching Ultimate FAQs posts
- Using a Gutenberg Query Loop may require forcing Relevanssi to activate
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 );