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

OceanWP

…$post->blog_id ); } ?> <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> <div class=”search-entry-inner clr”> <?php // Featured Image. get_template_part( ‘partials/search/thumbnail’ ); ?> <div class=”search-entry-content clr”> <?php // Title. get_template_part( ‘partials/search/header’ ); // Content. get_template_part( ‘partials/search/content’ ); // Read more button. get_template_part( ‘partials/search/readmore’ ); ?> </div><!– .search-entry-content –> </div><!– .search-entry-inner –>……file partials/search/content.php with this content: <?php /** * Search result page entry content * * @package OceanWP WordPress theme */ // Exit if accessed directly. if ( ! defined( ‘ABSPATH’ ) ) { exit; } global $post; // Excerpt length. $length = apply_filters( ‘ocean_search_results_excerpt_length’, ’30’ ); ?> <div class=”search-entry-summary clr”<?php…?>” title=”<?php the_title_attribute(); ?>” rel=”bookmark”><?php function_exists( ‘relevanssi_the_title’ ) ? relevanssi_the_title() : the_title(); ?></a> </h2> </header> This template uses the relevanssi_the_title() function to print out the title to make sure the highlights are included. Multisite searching If you want to use multisite searching with OceanWP, it’s easiest to make the template…

Indexing product codes with and without spaces

A Relevanssi Premium customer had a problem with product codes. The codes are in the format “ABC 100”, a group of letters and digits with a space in between. Users may search for the codes without the space, so the post should be found with “ABC 100” or “ABC100”. The……product can be found with either “ABC 123” or “ABC123” search * terms. This is applied to post content and titles. * * @param string $content The content to manipulate. * * @return string The content with the spaceless product codes added. */ function rlv_product_codes( $content ) { $pattern =…

Ordering search results by date
I’m building a directory and would like the oldest posts to automatically display first in the search results, is this…

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