Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search bar to ignore spaces
This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 9 months ago.
- AuthorPosts
- June 14, 2021 at 1:55 pm #33652
Hi,
I would like my search bar to ignore spaces. For example, I would like 2020 Case 1 and 2020Case1 to return the same search results, is this possible? If so, how do I do it?
Thanks!
-Alison
June 15, 2021 at 12:58 pm #33660Hi,
Well, with a simple custom code it is possible to remove the spaces from the phrase, so the search term “2020 Case 1” becomes “2020Case1”. Based on this knowledge base, the code is:
Best,add_filter('asp_search_phrase_before_cleaning', 'asp_replace_characters', 10, 1); add_filter('asp_query_args', 'asp_replace_characters', 10, 1); function asp_replace_characters( $s ) { $characters = " "; // Type characters one after another $replace_with = ''; // Replace them with this (space by default) if ( is_array($s) ) { if ( isset($s['s']) && !$s['_ajax_search'] ) $s['s'] = str_replace(str_split($characters), $replace_with, $s['s']); } else { $s = str_replace(str_split($characters), $replace_with, $s); } return $s; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 15, 2021 at 2:49 pm #33674Hi,
I added that custom code via the code snippets plugin but it is not working. I would like my users to search 2019 Case 1 or 2019Case1 and get the same results.
Thansk!
-Alison
June 15, 2021 at 3:19 pm #33675Hi Alison,
In this specific case, does the keyword “2019Case1” exist somewhere in the post content or in the selected fields? If it does not exist, then this is not going to be possible.
All that code does is removes the spaces from the phrases entered, and then matches the resulting keyword agains the selected fields.I think what you mean is somehow detecting when the user did not enter the spaces and account for them – basically the other way around. Unfortunately that is not possible. There is no way to tell from the code perspective where the spaces should be, and trying all the variations would simply overload the server within seconds.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.