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

How to include specific posts
Hello, I’m using WooCommerce and Relevanssi and they work great.My Site Search only searches Products, however I want to let…
Blocking pre and code tags
If your posts have lots of programming code examples in <pre> and <code> tags, those might look pretty bad in…

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