Skip to main contentSkip to footer

A good question was asked in the post Natural-language date queries:

Is it possible to extend this so that I can simply add a the year in the search and only results from that year would show?

The answer is yes! Add these functions to your site:

add_filter( 'relevanssi_modify_wp_query', function( $query ) {
    $m = preg_match( '/\b\d{4}\b/', $query->query_vars['s'], $matches );
    if ( 1 === $m ) {
        $year = $matches[0];
        if ( $year > 1900 && $year < 2100 ) {
            global $relevanssi_original_query;
            $relevanssi_original_query = $query->query_vars['s'];

            $query->query_vars['year'] = $year;
            $query->query_vars['s']    = str_replace( $year, '', $query->query_vars['s'] );
        }
    }
    return $query;
} );

add_filter( 'posts_pre_query', function( $a, $b ) {
    global $wp_query, $relevanssi_original_query;
    if ( ! empty( $relevanssi_original_query ) ) {
	    $wp_query->query_vars['s'] = $relevanssi_original_query;
    }
    return $a;
}, 100, 2 );

The code consists of two functions. The first one checks the search query. If there’s a single four-digit number between 1900 and 2100 (change those numbers to whatever is reasonable for your site), the number is removed from the search query and converted into a year parameter.

The second function restores the search query to what it was before the number was removed, so the user doesn’t have to wonder why their search query changed.

Your account

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

Search

Popular Resources

WooCommerce: Return only exact matches for SKU searches

…_sku custom field (because otherwise, SKU search will not find anything), and the search query must match the SKU exactly. Add this code to your site: add_filter( ‘relevanssi_hits_filter’, ‘rlv_sku_exact_match’ ); function rlv_sku_exact_match( $hits ) { global $wpdb; $post_ids = $wpdb->get_col( $wpdb->prepare( “SELECT post_id FROM $wpdb->postmeta WHERE meta_key = ‘_sku’ AND……)[‘object’]; return in_array( $hit_object->ID, $post_ids, false ); } ); return $hits; } Any searches that match an SKU in the database should only return the product matching the SKU. If you index product variation SKUs for the main product, the function above won’t work. When searching for product variation SKUs,…This little filter function works on relevanssi_hits_filter. When a search query is made that matches an SKU (or any other custom field, but SKUs are the most likely scenario here), only results that match the SKU will be returned. For this to work, Relevanssi must be set to index the…

Indexing attachment file names

Relevanssi has been working nicely for the normal usecase. But how does one setup indexing of attachment files. When someone searches by a file name or an extension like pdf, there are no results. I have enabled ‘attachment’ at Relevanssi ‘Indexing options’ and still there are no results. Relevanssi doesn’t……index attachment file names. For attachments, Relevanssi indexes title and description. However, adding the file name to the Relevanssi index is simple using the relevanssi_content_to_index filter hook. Add this function to your site and rebuild the index: add_filter( ‘relevanssi_content_to_index’, ‘rlv_add_filenames’, 10, 2 ); function rlv_add_filenames( $content, $post ) { if……( ‘attachment‘ === $post->post_type ) { $content .= ‘ ‘ . basename( $post->guid ); } return $content; } Notice that by default Relevanssi replaces periods with spaces, so “sample.pdf” is indexed as “sample pdf”. That should not be a problem, as also in searching “sample.pdf” becomes “sample pdf”. However, if…

Proximity sorting

…flexible but requires some work. It is not complicated; you must tell Relevanssi where to find the necessary information. Relevanssi works with latitude and longitude values and needs the coordinates in the “61.4898, 23.7735” format, the most common format. Setting up proximity sorting First, you need to enable proximity sorting….Relevanssi Premium 2.16 added proximity sorting to search results. That means you can sort the search results by geographic location. Instead of having the most relevant matches first, you can have the nearest results first. There are many ways to use geolocation in WordPress. The way Relevanssi does this is……Calculating the distances is an extra step, and it’s not turned on by default. Add this to your site to enable proximity sorting: add_filter( ‘relevanssi_proximity_sorting’, ‘__return_true’ ); The next step is to declare the comparison coordinates. Where is the spot against which Relevanssi compares the results? These coordinates can be…

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