Skip to main contentSkip to footer

In regular expressions, ^ can be used to match the beginning of the line and $ to match the end of the line. For example, ^ban will match banana, but not urban, while ban$ matches urban, but not banana. These are called anchoring operators.

By default, Relevanssi matches either beginning or the end of the word, but not in the middle – ban will match banana or urban, but not abandon. If you want more control over this in the search, you can add support for the ^ and $ operators in Relevanssi using the relevanssi_fuzzy_query filter hook.

Add this to your site:

add_filter( 'relevanssi_fuzzy_query', 'rlv_edge_operators' );
function rlv_edge_operators( $query ) {
    if ( isset( $_REQUEST['s'] ) && '^' === substr( $_REQUEST['s'], 0, 1 ) ) {
        return "(relevanssi.term LIKE '#term#%')";
    }
    if ( isset( $_REQUEST['s'] ) && '$' === substr( $_REQUEST['s'], -1 ) ) {
        return "(relevanssi.term_reverse LIKE CONCAT(REVERSE('#term#'), '%'))";
    }
    return $query;
}

This is a very simple version that works best with single-word searches: it always looks at the first and last character of the whole search query. So, if you search for ^ban foo, it’s the same as searching for ^ban ^foo and searching for ban ^foo is the same as searching for ban foo.

Creating a smarter version that analyzes the search query and offers support for using different operators for individual words in the query is left as an exercise for the reader.

Your account

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

Search

Popular Resources

Indexing trashed posts
By default, Relevanssi does not index trashed posts. If you want to enable searching for trashed posts in the WordPress…
CM Tooltip Glossary
A Relevanssi Premium customer bumped into a weird problem with CM Tooltip Glossary (and probably Elementor). Using Elementor, CM Tooltip…
MemberPress Downloads add-on

The Downloads add-on for MemberPress adds downloadable files to MemberPress. These files are stored outside the Media Library, so by default Relevanssi attachment indexing has no access to them. However, Relevanssi offers filter hooks you can use to make Relevanssi index the MemberPress Downloads files. Adjust the query First, we……need to tell Relevanssi to look for mpdl-file posts instead of attachment posts using the relevanssi_get_attachment_posts_query filter hook. By default, Relevanssi looks for posts with the post type attachment, post status inherit and a suitable MIME type. These all need to be changed, because the Downloads posts have the post……to modify the name and path of the file sent to Relevanssi: add_filter( ‘relevanssi_get_attached_file’, ‘rlv_mpdl_file’, 10, 2 ); /** * Filters the name and path of the file. * * Gets the file name from the _mpdl_file_filename custom field of the download file * post and corrects the path, too….

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