Skip to main contentSkip to footer

My Relevanssi engine has the AND operator enabled, with a fallback to OR when the search finds no matches. I’d like the search to display first the AND results and later the OR results.

For example, if I search for “wedding party”, I’d like the search to show first the entries matching both words and later the entries matching only “wedding” or only “party”.

Is it possible to configure or program a search like that?

What you’re asking for is pretty much what the OR search aims to do. It’s not strict, though, so if some post seems to be valuable even if it doesn’t have all the search terms, it will be high on the list, but most of the time if all search terms are good search terms, AND matches will be on the top.

If you really want to push it further, perhaps something like this with the relevanssi_match filter hook:

add_filter( 'relevanssi_match', 'rlv_and_boost' );
function rlv_and_boost( $match ) {
    global $rlv_and_database;
    $multi_term_boost = 5;
    if ( ! isset( $rlv_and_database[ $match->doc ] ) ) {
        $rlv_and_database[ $match->doc ] = true;
    } else {
        $match->weight *= $multi_term_boost;
    }
    return $match;
}

With this function in place, posts that match more than one search term will get a bonus. The more search terms the post matches, the bigger the bonus. You can adjust the boost factor until you get results you like.

This was asked here.

Your account

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

Search

Popular Resources

Polylang attachment searching
If Polylang is in use and you haven’t enabled “Media translation“, your attachment files won’t have a language. That’s fine,…
Problems with highlighting in post content

Versions 2.27.5 (Premium) and 4.24.4 (free) added extra security to highlighting search terms in post content. Before that, an attacker with contributor access could inject malicious content in the posts and the highlighting would then trigger that. This is now fixed, but the fix comes with a cost: the post……efficient in-page search feature that can be used to find the matching content. Another option is to adjust the wp_kses_post() functionality with the wp_kses_allowed_html filter hook. If you need to allow CSS styles, for example, you can use this snippet: add_filter( ‘wp_kses_allowed_html’, function( $html, $context ) { if ( ‘post’…

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