With Relevanssi Premium, it would be as simple as setting the operator query varible to “OR” if the word “or” appears in the search query.
With the free version, you can use the relevanssi_search_filters filter hook to change the operator
add_filter('relevanssi_search_filters', 'rlv_change_the_operator');
function rlv_change_the_operator($params) {
if (word OR appears in the search query) {
$params['operator'] = "OR";
}
return $params;
}
Something like that.
Thanks for your quick reply. We bought the premium now for this purpose and quicker ‘did you mean?’ results. I’ll continue this convo here for continuity.
Now that we have premium, how do we go about ‘setting the operator query variable to “or” if the word “or” appears in the search query’? Not seeing any setting that would allow this nor any for more info in the docs.
Thanks much!
add_filter('pre_get_query', 'rlv_set_operator');
function rlv_set_operator($query) {
if (stripos($query->query_vars['s'], " or ") !== false) $query->set('operator', "OR");
}
Something like that, I suppose.
Thanks. I have this as the code now, which I think is what you were outlining…
add_filter('pre_get_posts', 'rlv_set_operator');
function rlv_set_operator( $query ) {
if ( stripos( $query->query_vars['s'], " or " ) !== false ) {
$query->set( 'operator', "OR" );
}
}
I don’t quite understand what the $query->set(‘operator’, “OR”) part does or works as I don’t see any reference to an ‘operator’ in var_dump($query). Is it supposed to be ‘relationship’ or something perhaps?
Thanks again.
$query->set('operator', 'OR'); should set the query variable “operator” to “OR”. It should be in $query->query_vars['operator'] (so you can also directly set $query->query_vars['operator'] = "OR";, too).
If it’s not happening, I’d check that the conditional is working as it should.
Alrighty, thanks much for your help. I don’t quite understand as can’t find any info on the ‘operator’ query_var, but that’s ok. It seems to be working as intended so I understand enough for now!
Thanks again.
operator is a Relevanssi-specific query variable that accepts two different values, OR and AND, and sets the search operator. It only works in Relevanssi Premium.