Alternate word searches

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
6 years, 4 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5843

    Participant

    Can you point me in the right direction for creating alternative word searches such as, for example, a search for roster would be treated as a search for schedule?

    Thanks,
    Tony

    #5846

    Hi!

    If I understand correctly, You want to replace specific words by alternatives. The only possible way right now is to add a custom filter function to modify the input text silently before it hits the database. A possible function would be:

    add_filter('asp_search_phrase_after_cleaning', 'asp_alternate_words', 1, 1);
    function asp_alternate_words( $s ) {
      
      // An array of the originals you want to replace
      $originals = array(
        "word1",
        "word2",
        "wordN"
      );
      
      /* Array of the alternatives
       * .. must be the same number as the originals
       * "word1" is replaced with "alt1"
       * "word2" is replaced with "alt2" etc..
       * */      
      $alternatives = array(
        "alt1",
        "alt2",
        "altN"
      );
      
      return str_replace($originals, $alternatives, $s);
      
    }

    Put this to your themes functions.php file. As you can see from the code comments you can add as many words and replacements as you need.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.