• Resolved eternalstudent

    (@jayeshtaparia)


    Hi,

    Is there any way that the search can ignore all special characters.

    For example, my post has the title ABCD#1234. Somebody is searching for ABCD1234 on my website…then the post ABCD#1234 should come up?
    I mean all special characters like ‘,”,/,@,:,., etc.

    Best regards,

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    The search does ignore the special characters, but by default, they’re replaced with spaces, so “ABCD#1234” becomes “ABCD 1234”. If you want it to read as “ABCD1234”, you can add this to your site:

    add_filter( 'relevanssi_punctuation_filter', 'rlv_adjust_punctuation' );
    function rlv_adjust_punctuation( $replacements ) {
        $replacements['#'] = '';
        return $replacements;
    }

    This will make Relevanssi remove # instead of replacing it with spaces. You can use similar method for all other punctuation you want to adjust. If you want to change the default replacement from space to removal, you can do all at once:

    add_filter( 'relevanssi_default_punctuation_replacement', function() { return ''; } );

    Both of these functions go to theme functions.php and you then need to reindex for the changes to take effect.

    Thread Starter eternalstudent

    (@jayeshtaparia)

    Hi,

    1) Thanks for the response. I don’t know where to add this code. I use the wordpress.com business plan and everything comes pre-installed for me ??
    But we will come to that later…

    2) My website has ABCD 1234 and ABCD#1234. Somebody searches for ABCD1234…so both ABCD 1234 and ABCD#1234 should come up.

    So can it be possible…search ignoring both spaces and all special characters…

    Plugin Author Mikko Saari

    (@msaari)

    It’s highly likely that if you ignore all the spaces, your search will break. Or is it only about these product codes? If it is, then removing spaces might just be fine.

    In any case, the best solution depends on the exact nature of the product codes. How easy it is to recognise a product code? Do they all have the same pattern, or are there different patterns? You probably want a solution where all the product codes are indexed without any spaces or special characters and all the product code searches all also converted the same way.

    Thread Starter eternalstudent

    (@jayeshtaparia)

    Yes, just don’t into account any special characters and spaces.

    If I have ABCD%1234 (or any other special character or a space anywhere in between) on my website…and the person searches for A!C %D/123+D
    …the result should always show ABCD%1234.

    Plugin Author Mikko Saari

    (@msaari)

    I’m on my phone so I can’t write code, but yeah, use the both functions from my first message, but in the first one replace the # with a space. Then rebuild the index, and that should do it, but be warned it will remove literally every single space from your posts for Relevanssi.

    The functions need to go somewhere they will be executed from. Theme functions.php file is common, or you can use a Code Snippet plugin or something like that. I don’t know what works on WordPress.com, their support can help you out with that.

    Thread Starter eternalstudent

    (@jayeshtaparia)

    add_filter( ‘relevanssi_default_punctuation_replacement’, function() { return ”; } );

    Hi,

    I think I have managed to install the above code using the snippets plugin and it seems to be working.

    Search seems to be ignoring all the special characters and spaces for now. But it is not ignoring the character °

    So do I have to install the below code for this special character:

    `add_filter( ‘relevanssi_punctuation_filter’, ‘rlv_adjust_punctuation’ );
    function rlv_adjust_punctuation( $replacements ) {
    $replacements[‘°’] = ”;
    return $replacements;
    }`

    Or can you do something to Relevanssi from your side so that it ignores ° ?

    Plugin Author Mikko Saari

    (@msaari)

    There’s no special handling for °, but looks like regular expressions don’t count it as punctuation. The relevanssi_default_punctuation_replacement filter is used to convert everything that’s punctuation (using /:punct:/). ° is not punctuation, thus it’s not converted.

    So yes, you need to handle it separately with relevanssi_punctuation_filter.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘ignore special characters’ is closed to new replies.