Optimize WP search query term

Question

I made the following filter to remove s from terms shirts or hoodies, however I don’t think it’s the way it should be. Any ideas if I can make it in a more proper way?

add_filter('posts_where',function($query){
 //we check if the search term has enough length & contains "shirts" or "hoodies" then remove "s" from the end of the words.
 if (isset($_GET["s"]) 
     and substr(strtolower($_GET["s"]),-6)=='shirts' 
     and strlen($_GET["s"])>5) {
   $query=str_replace($_GET["s"],
                      substr($_GET["s"],0,-1),
                      $query);
 }
else if (isset($_GET["s"])
     and substr(strtolower($_GET["s"]),-7)=='hoodies' 
     and strlen($_GET["s"])>6) {
   $query=str_replace($_GET["s"],
                      substr($_GET["s"],0,-1),
                      $query);
 }
    return $query;
});

Also, is there any way to include a clause in the above function when someone type tshirt to show results for t-shirt and same for tshirts to show results for t-shirt?

Basically my idea is the following:
All of these terms (t-shirts,shirt,shirts,tshirt,tshirts) to display results for term t-shirt.
And hoodies to simply display results for hoodie in singular form.
Thank you!

0
Korla 4 months 0 Answers 17 views 0

Leave an answer