Skip to main contentSkip to footer

Relevanssi has a minimum word length setting that is set to three characters. Any one- or two-letter words are not indexed unless you change this setting from the Relevanssi advanced indexing settings. Sometimes it would be helpful to ignore short words but to index one- and two-digit numbers.

You can do that with a bit of help. Adjust the minimum word length to 1 or 2 depending on your needs, add this function to your site and rebuild the index:

add_filter( 'relevanssi_remove_punctuation', function( $str) {
    return implode(
        ' ',
        array_filter(
            explode( ' ', $str ),
            function( $word ) {
                if ( relevanssi_strlen( $word ) < 3 ) {
                    return is_numeric( $word );
                }
                return true;
            }
        )
    );
} );

This function will let short numbers through while blocking short words.

Your account

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

Search

Popular Resources

Oxygen and Ninja Tables
Relevanssi has support both for Oxygen and Ninja Tables, but the way the Ninja Tables support is done, it’s not…
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:

3 Comments. Leave new

  • I’m not sure this snippet is working. After adding it to my functions.php file, and rebuilding the index, searching for two-digit numbers still returns “No Results”

    Default view (no search): https://tppr.me/5Ymd1D (notice the two and three-digit office numbers marked on the screenshot)
    Three-digit number search: https://tppr.me/QxdmvB (returns results)
    Two-digit number search: https://tppr.me/Cmlr9c

    Is there a chance that the provided function (that should enable indexing < 3 character words if they are numerical) is not working?

    Reply
    • Daniel, did you also adjust the minimum word length to 2 from the advanced indexing settings?

      The function works for two-digit numbers, but in your case, it’s possible this function runs before the one that removes punctuation, so “2-46” is not a two-digit number when this function runs. Try changing the priority of this function to bigger than 10 (add_filter( 'relevanssi_remove_punctuation', function( $str) {...}, 11 );).

      Reply
      • Hi Mikko, thank you for the prompt reply.

        Changing the priority of the function solved the issue.

        Thanks once again, appreciate the quick response!

        Reply

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