1

I've got a custom post type for comic books. The post type is called Comics (comics). I've implemented the WordPress search and I'm able to search the custom post type fine with the standard search form using the post_type hidden field.

I've come across an issue where some people may search for Spider-Man but instead of typing Spider-Man they will type Spiderman or possibly even Spider man. My comic book titles are stored as Spider-Man and they don't return in the results if the hyphen isn't in place.

Do I need to implement some sort of custom WordPress search functionality? Any pointers would be great.

2
  • I would note that if you have the ability to use elastic search it will provide an enormous boost in both performance and search quality
    – Tom J Nowell
    Jun 26 '18 at 13:28
  • Thanks for your comment, I’ll take a look at that see what I can find.
    – sims
    Jun 26 '18 at 21:05
1

It is possible to filter search terms before they are submitted to the actual query using the query_vars hook. So in your case you would do something like this:

add_filter ('query_vars', 'wpse307005_filter_search', 10, 1);
function wpse307005_filter_search ($args) {
  preg_replace ('Spiderman','Spider-Man',$args[s]);
  preg_replace ('Spider man','Spider-Man',$args[s]);
  return $args;
  }

Where $args[s] holds the search string.

4
  • Thanks for your answer. Am I right in saying that I would have to do this for every variant of Spider-Man i.e spiderman, Spiderman, Spider Man, spider man, Spider man... I couldn't get your answer to work exactly but I did use pre_get_posts() to get it working.
    – sims
    Jun 26 '18 at 19:40
  • You could have a more complex regex tot catch all varieties, i suppose, but i'm not very good at that.
    – cjbj
    Jun 26 '18 at 20:51
  • I was thinking that, I’m no good at the Regec either but I’ll have a look and give it a go. I’ll update my post with an answer to the regex if I happen to get it working. Thanks for your help on this.
    – sims
    Jun 26 '18 at 21:05
  • Don't edit the post, but add it as an answer and accept it (or +1 my post if you found it helpful). Else this question will keep popping up as unsolved.
    – cjbj
    Jun 27 '18 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.