Skip to main contentSkip to footer

Is is possible to set Relevanssi in a way that it would return only perfect matches for queries up to 3 characters, but any words that starts with the queried term if it has 4 characters or more?

Originally asked here

Yes, but it requires a bit of code.

add_filter( 'relevanssi_term_where', 'rlv_three_exact_four_fuzzy', 10, 2 );
function rlv_three_exact_four_fuzzy( $where, $term ) {
    if ( relevanssi_strlen( $term ) <= 3 ) {
        $where = "(relevanssi.term = '$term')";
    }
    return $where;
}

Add this to your site and set Relevanssi to use partial matching in the searching settings. Now searches with search terms that are at most three characters long will use exact matching.

The way this works is simple. The relevanssi_term_where filter hook lets you adjust the part of the MySQL query that includes the search term. If the search term is short, instead of doing the default partial search query, the function replaces that with the exact match query (relevanssi.term = 'term').

Your account

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

Search

Popular Resources

OceanWP

OceanWP is a popular WordPress theme. It works fine with Relevanssi, but how it handles excerpts on the search results pages is not fully compatible with Relevanssi. Relevanssi expects the theme to use the_excerpt() to print out the excerpts. OceanWP does that, but only for posts that have hand-made excerpts….…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…

WooCommerce: Popularity and price sorting

…as easily as the default WP search does. This function handles price, popularity and rating sorting: add_filter( ‘relevanssi_orderby’, ‘woocommerce_relevanssi_orderby’ ); function woocommerce_relevanssi_orderby( $orderby ) { if ( in_array( $orderby, array( ‘price‘, ‘price-desc’ ), true ) ) { global $wp_query; $orderby = ‘meta_value_num’; $wp_query->query_vars[‘meta_key’] = ‘_price’; } if ( ‘price‘ ===…Many WooCommerce users use search sorting that allows users to sort by popularity or price. Unfortunately, while Relevanssi works fine with WooCommerce, those sorts do not work. Relevanssi doesn’t know about price or popularity, and the sorting assumes there’s a default WP search underneath. Relevanssi doesn’t do meta field sorting…

WP Event Manager
Using Relevanssi with WP Event Manager requires you to adjust the search process in WP Event Manager a bit. Fortunately,…

Related Posts:

Comment Section:

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