Skip to main contentSkip to footer

Relevanssi Premium has support for in-query Boolean operators. Relevanssi uses + and – for the operators: cats -dogs and cats +kittens.

If you prefer instead to use the verbal AND and NOT operators, it only takes one small function. Add this to your site:

add_filter( 'relevanssi_search_filters', 'rlv_boolean_operators' );
function rlv_boolean_operators( $args ) {
	$args['q'] = str_replace( ' and ', ' +', $args['q'] );
	$args['q'] = str_replace( ' not ', ' -', $args['q'] );
	return $args;
}

This function (on the relevanssi_search_filters hook) looks at the search query and replaces the word “AND” with a + sign and the word “NOT” with a – sign. A search query can now say “cats NOT dogs“, and that gets interpreted as “cats -dogs“, which Relevanssi understands.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Indexing term slugs
I have a term who name is for, example, Great Blue Trojans. The slug is GBT.I want to be able…
Menu problems in multisite search

Sometimes multisite searches cause problems in navigation menus. This is caused by pages from another subsite going into the page cache, and then replacing a page in the navigation menu with the same page ID. If you’re having this problem, adding the following code to the theme functions.php should solve……the issue. add_action( ‘wp_head’, ‘rlv_uncache_menu_posts’, 99 ); function rlv_uncache_menu_posts() { if ( is_search() ) { global $wpdb; $ids = $wpdb->get_col(“SELECT meta.meta_value FROM $wpdb->posts as posts, $wpdb->postmeta as meta WHERE posts.ID = meta.post_id AND posts.post_type = ‘nav_menu_item’ AND meta.meta_key = ‘_menu_item_object_id'”); if ( is_array( $ids ) ) { foreach ( $ids…

Popular searches
This is a very common request: “How do I show the most popular searches in a widget or on a…

Related Posts:

Comment Section:

6 Comments. Leave new

  • Hi Mikko, I tested using the AND operator and using 2 terms, like: ‘ cars and bikes’ is throwing results with one of each words. Maybe it is because Im using the free version?
    Is this working fine on the Premium?
    thanks!

    Reply
    • Gus, there’s no AND operator like that in Relevanssi. You choose the operator from the searching settings. If you set the operator to “AND” from the searching settings and search for “cars bikes”, you will get results that have both words.

      In Premium, you can set the operator to OR from settings and then do a search for “cars +bikes” or “bikes +cars” to get results with both words.

      Reply
  • Is there an OR operator if you change the default to AND?

    Reply
  • Is there an operator to use where you only want posts that have x AND y in their keywords, not all posts with x OR y in them?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed