No results for "M&M", but results for "M&"

Home Forums Product Support Forums Ajax Search Pro for WordPress Support No results for "M&M", but results for "M&"

This topic contains 6 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 4 years, 6 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24143
    tomclayson92
    tomclayson92
    Participant

    One of the main product we expect to be selling is M&M’s (the chocolate candy). When users search for this however, no results appear. If I search for just “M&”, then it works correctly. Why might this be?

    #24149
    tomclayson92
    tomclayson92
    Participant

    This appears to be happening due to WPML’s treatment of the ‘&’ character. Searching for M&M works correctly, but obviously no user will know that. I have had similar issues with WPML before, and they haven’t been helpful, so is there any way to convert characters like & into ‘&’ before searching if a translated language is being used?

    #24157
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Sure, I think we can do something about this. The issue seems to be indeed the ‘&’ character storage as HTML entity. There is this knowledge base article, that allows replacement of some characters with others. In your case the code will be:

    This should hopefully do the trick.

    Best,
    Ernest Marcinko

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


    #24168
    tomclayson92
    tomclayson92
    Participant

    Thanks for the snippet, but that hasn’t quite seemed to work. It is replacing the character, but still no search results, and now the original ‘M&’ search, which was working, has also stopped working.

    #24171
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    At the moment, I am seeing results like so: https://i.imgur.com/TX3ta4k.png
    Was there any change anywhere?

    Best,
    Ernest Marcinko

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


    #24179
    tomclayson92
    tomclayson92
    Participant

    With the same query, I don’t get any results. I didn’t change anything to do with the search yesterday.

    Attachments:
    You must be logged in to view attached files.
    #24186
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I see the issue now, it is not the ‘&’ character, but the apostrophe.

    Searching for:

    M&M’s

    ..returns results, but searching:

    M&M's

    ..does not. It’s a different apostrophe character. Assuming that every apostrophe is stored like that, I would suggest this custom code then:

    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; 
    }
    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.