Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Partial matching starts with
- This topic has 6 replies, 2 voices, and was last updated 3 years, 9 months ago by
Ernest Marcinko.
-
AuthorPosts
-
August 25, 2022 at 9:30 am #39013
alinahomecare
ParticipantHi, I’m having an issue when someone searches if they don’t use a space between keywords.
I want a search that starts with my additional search tags to match the result, but it isn’t working.
For instance, I have an additional search tag of “KT22” on one page. If someone searches “KT22 8DN”, it works. If someone searches “KT228DN” it doesn’t work. Is there a way to match it based on the first 4 letters of the search?
August 25, 2022 at 12:23 pm #39024Ernest Marcinko
KeymasterHi,
Well the problem with this is, that partial matching works the other way around, the phrase is partial matched agains the content. Otherwise all words from all of the posts and all of the fields would have to be matched agains the keyword to see if it partial matches – which is of course not possible.
Theoretically, I can suggest a small custom code snippet, which “breaks” the search phrase at the character position 4, by adding a space character to it.
Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
add_filter('asp_query_args', 'asp_query_args_search_at_four', 10, 1); function asp_query_args_search_at_four($args) { $args['s'] = substr_replace($args['s'], ' ', 3, 0); return $args; }August 25, 2022 at 12:32 pm #39025alinahomecare
ParticipantHi, thanks for this but unfortunately it doesn’t quite solve my issue as it means the search results for any words now pull in a lot of different pages because the 4 characters aren’t always specific enough.
Is there any way of changing the code so it searches by the first 4 characters but doesn’t add the space? Or is there a way to just search by ‘starts with’?
August 25, 2022 at 12:34 pm #39027Ernest Marcinko
KeymasterSure, that is even a bit simpler:
add_filter('asp_query_args', 'asp_query_args_search_at_four', 10, 1); function asp_query_args_search_at_four($args) { $args['s'] = mb_substr($args['s'], 0, 4); return $args; }August 25, 2022 at 12:38 pm #39028alinahomecare
ParticipantAmazing! thanks
August 25, 2022 at 3:42 pm #39034alinahomecare
ParticipantHi, I’m having a new issue due to this. Now that it is matching against the first 4 characters, it is showing some pages I don’t want it to.
I then add a negative keyword, but I think these aren’t working because the negative is only matching against the first 4 characters. Is there a way to match negatives against more than 4 characters but positives against just 4?
Thanks
August 26, 2022 at 8:54 am #39041Ernest Marcinko
KeymasterI’m afraid there is no way around it. If the search phrase is changed, then that becomes the search phrase, so everything is based on that.
-
AuthorPosts
- You must be logged in to reply to this topic.