Skip to main contentSkip to footer

On one of our sites we have 2 search boxes, one where you can do a normal search and the other where you can search for employees or departments. The employees/department search is done by using the relevanssi_do_query() function.

Can we use fuzzy matching on the normal search and disable it on the employees/department search?

Yes, you can, but it takes some hacking. Since Relevanssi has just one setting for the fuzzy search, you need to change it on the fly for one of the searches. It’s probably easier to do with the relevanssi_do_query() search.

Relevanssi reads the fuzzy search setting from the relevanssi_fuzzy option. WordPress provides you with pre_option_relevanssi_fuzzy filter you can use to change the value. The values are “always” and “sometimes”, and everything else counts as none. So, set the setting to always from Relevanssi settings, and then add a filter function that sets the value to, for example, “none” when you’re doing an employee/department search.

For example, you can set a global variable before the relevanssi_do_query(), like this:

global $relevanssi_disable_fuzzy;
$relevanssi_disable_fuzzy = true;
relevanssi_do_query( $args );

Then use that global variable as a trigger:

add_filter( 'pre_option_relevanssi_fuzzy', 'rlv_disable_fuzzy' );
function rlv_disable_fuzzy( $value ) {
    global $relevanssi_disable_fuzzy;
    if ( $relevanssi_disable_fuzzy ) $value = "none";
    return $value;
}

The pre_option_ filter hooks are, in general, a mighty powerful tool to know.

Your account

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

Search

Popular Resources

Disabling Relevanssi
If you need to disable Relevanssi, for example for a particular template, all you need to do is to unhook…
Excluding protected posts

A site I’m working with has a handful of “protected” posts (password required to view). For various reasons, we don’t want to show these in search results. While there is no excerpt shown, I’d prefer for people to not even know they exist. Relevanssi sees protected posts as public posts……(because their post status is publish, not private like with private posts). That’s what they are, as, in general, users will be able to see protected posts. Just not their content unless they know the password. Relevanssi does protect the content and won’t show parts of it in excerpts. If……you want to deindex protected posts completely, add this code to your site: add_filter( ‘relevanssi_indexing_restriction’, ‘rlv_exclude_protected’ ); function rlv_exclude_protected( $restriction ) { global $wpdb; $restriction[‘mysql’] .= ” AND post.post_password = ””; $restriction[‘reason’] .= ‘ Has a password’; return $restriction; } Then just rebuild the index. This will prevent Relevanssi from…

Search shortcode

Relevanssi adds a shortcode to help making links to search results. That way users can easily find more information about a given subject from your blog. The syntax is simple: John Doe This will make the text John Doe a link to search results for John Doe. In case you…

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