Partial matching starts with

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Partial matching starts with

This topic contains 6 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 7 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #39013
    alinahomecare
    alinahomecare
    Participant

    Hi, 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?

    #39024
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39025
    alinahomecare
    alinahomecare
    Participant

    Hi, 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’?

    #39027
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure, 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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39028
    alinahomecare
    alinahomecare
    Participant

    Amazing! thanks

    #39034
    alinahomecare
    alinahomecare
    Participant

    Hi, 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

    #39041
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I’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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.