• Resolved wpcrank_phil

    (@wpcsphil)


    Hello,

    Sorry if this has been answered before – but I couldn’t find it anywhere.

    My client would like for the default search result to be AND based, requiring all the words to return a result and its currently setup and running properly this way. He would now like if someone enters the text ‘or’ into the search field that the search now returns OR results.

    How can we make it do this?

    Thanks much!

    Philip

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    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.

    Thread Starter wpcrank_phil

    (@wpcsphil)

    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!

    Plugin Author Mikko Saari

    (@msaari)

    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.

    Thread Starter wpcrank_phil

    (@wpcsphil)

    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.

    Plugin Author Mikko Saari

    (@msaari)

    $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.

    Thread Starter wpcrank_phil

    (@wpcsphil)

    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.

    Plugin Author Mikko Saari

    (@msaari)

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘“OR” as a keyword’ is closed to new replies.