How To Auto-Redirect When Search Query Returns Only One Match In WordPress

GET PREMIUM WORDPRESS PLUGINS & THEMES AT 90% DISCOUNT

How-to-Auto-Redirect

Disclosure: This content is reader-supported. If you click on our links, we may earn a commission.

Picture of Munish Siwatch
Munish Siwatch

Web Developer & Writer

How To Auto-Redirect When Search Query Returns Only One Match In WordPress. Sometimes is the little things that make a big difference. Saving your visitors from a click here and a scroll there can make the user experience just a little more enjoyable. This can further help in boosting the conversion rates as well.

If you provide your visitors with the ability to search your website, there will be times when the results returned might be just a single page or post. In those situations, it can be helpful to direct visitors to the individual post instead of to a search results page where they must then click through to a single result.

How-to-Auto-Redirect-When-WordPress-Search-Query-Only-Returns-One-Match

In this WordPress tutorial, we’ll show you how to set up your website to perform an auto-redirect any time a user search query returns only one potential match.

How to Auto-Redirect When WordPress Search Query Only Returns One Match

This is a relatively easy improvement to make, but there are some things you should be aware of before you get started. The first issue is that your functions.php file is a critical file. If you make an error when editing this file, you could easily break your website–not good. Whenever possible you should avoid making changes directly to your functions.php file.

There are a few potential solutions to managing this challenge:

  1. Use a child theme and make any changes necessary to the child theme files.
  2. Use a WordPress plugin like Code Snippets which is available for free in the WordPress plugin repository.
  3. Create a plugin.

Ok, now that you have a better understanding of the importance of managing your custom functions properly. Let’s take a look at how you can make the required changes to your search functionality. We’re going to assume that we’re editing a child theme for this example. Here are the steps:

1.     Login to your cPanel, and open up your file manager

cPanel-File-Manager

2.     Create a Backup of Functions.php

backup-functions-php-file

Inside your child theme folder, select the functions.php file and click on copy. When the popup opens, under “Copy file to:” add the following: /functions-backup.php – this file will now serve as your backup in case something goes wrong with your original file. If that were the case, you could simply delete your broken file and rename the copy to just functions.php.

3.     Edit Functions.php

edit-functions-php-file

Next, select the functions.php file and click on Edit (click edit a second time when the popup box appears).
Scroll to the bottom of your file and paste the following code as seen in the picture below:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
  if (is_search()) {
    global $wp_query;
    if ($wp_query->post_count == 1) {
      wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
      exit;
    }
  }
}


4.     Paste the code

WordPress-search-redirect-functions-php

Paste the code above into the bottom of the functions.php file and save

Click Save in the top right corner and close the window.

Once you’re done that, head to your site and test out the search function. If you enter an obscure search term for which there is only one result, you should now find that you are redirected to the actual post instead of a search result. That’s it!

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook

New to creating a website? Join our newsletter.​

Let us do the work for you. Receive a bite-sized dose of AI, Tech, and Business news
delivered straight to your inbox.

Join over 38,500+ satisfied readers and subscribe to ByteBrief today!

Power Your Business

Some of the tools and services to help your business grow.

Software that's powerful, not overpowering. Seamlessly connect your data, teams, and customers on one CRM platform.

Try Hubspot

SentryPC combines award-winning computer monitoring features with essential content filtering, time management & access control.

Try SentryPC

85 thoughts on “How To Auto-Redirect When Search Query Returns Only One Match In WordPress”

  1. ballard13cramer.wikidot.com

    Every weekend i used to pay a quick visit this web site, for the reason that i wish for
    Knowledge, as this this website contains geenuinely nice material too.

  2. Good day very cool website!! Man .. Excellent .. Superb ..
    I’ll bookmark your website and take the feeds additionally?
    I am happy to search out so many helpful info here in the publish, we want develop extra strategies in this regard, thank
    you for sharing. . . . . .

  3. Hey! I just wanted to ask if you evr have any trouble with hackers?My laast blog
    (wordpress) was hacked and I ended up losing several weeks
    of hard work due to no data backup. Do you have any solutions to protect against hackers?

  4. Haave you ever thought about creating an e-book or guest authoring on other blogs?
    I have a blog centered on the same subjects you
    discuss and would love tto have you share some
    stories/information. I know my readers would appreciate your work.
    If youu are even remotely interested, feeel free to send me ann email.

  5. I every time emailed this weblog post page to all my friends, since if like to read it next my contacts willl too.

  6. Wayy cool! Some very valid points! I appreciae you penning this article plys the rest of the site is really
    good.

  7. Excellent post. I was checking clnstantly this blog and I am impressed!

    Very helpful info specially the last part 🙂 I care for such information a lot.
    I was seeking this ceertain information for a long time.
    Thank you and good luck.

  8. Really no matter if somwone doesn’t know after
    that its up to other users tht they will assist, so
    here it takes place.

  9. Your style is unique compared to other people I’ve
    read stuff from. I appreciate you for posting when you have the opportunity, Guess I’ll just
    bookmark this site.

  10. I think this is among the most important info for me. And
    i’m satisfied studying your article. But want
    to statement on some general things, The web site taste is perfect, the articles is in point of fact great : D.
    Just right process, cheers

  11. Thanks in favor of sharing such a fastidious opinion,
    article is pleasant, thats why i have read it fully

  12. I got this web page from my buddy who told
    me on the topic of this web site and at the moment this time I am
    visiting this website and reading very informative
    articles at this place.

  13. Admiring the time and energy you put into your blog and detailed information you present.
    It’s good to come across a blog every once in a while that isn’t the same out
    of date rehashed information. Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  14. you’re truly a good webmaster. The site loading pace is incredible.
    It seems that you are doing any unique trick.

    Moreover, The contents are masterpiece. you have performed a fantastic
    process in this matter!

  15. I think the admin of this website is really working hard
    for his web page, as here every information is quality based stuff.

  16. I’m not certain the place you are getting your info, but good topic.
    I must spend a while learning more or figuring out more.
    Thanks for wonderful information I was in search of this information for my mission.

  17. Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to
    your new updates.

  18. It is in reality a nice and useful piece of information. I’m glad that you shared this helpful information with us.

    Please keep us informed like this. Thanks for sharing.

  19. constantly i used to read smaller articles which also clear their motive, and that
    is also happening with this piece of writing which I am reading at this time.

  20. I seriously love your website.. Very nice colors &
    theme. Did you build this web site yourself?
    Please reply back as I’m looking to create my own personal website and want to find out where you got this from or just
    what the theme is named. Cheers!

  21. It is not my first time to pay a quick visit this site, i am
    browsing this web site dailly and take fastidious data from here daily.

  22. Wow, awesome weblog layout! How lengthy have you been blogging for?
    you made running a blog glance easy. The full look of your
    website is excellent, let alone the content!

  23. I’m more than happy to uncover this page. I need to to thank you for
    ones time for this particularly fantastic read!! I definitely
    enjoyed every bit of it and i also have you saved as a
    favorite to look at new things on your website.

  24. Wonderful article! That is the kind of information that are meant to be shared around the web.
    Disgrace on the search engines for now not positioning this post upper!
    Come on over and talk over with my site . Thank you =)

  25. Wow that was odd. I just wrote an extremely long comment but after
    I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyhow,
    just wanted to say wonderful blog!

  26. My brother suggested I would possibly like this blog. He was
    once totally right. This publish actually made my day.
    You cann’t believe just how much time I had spent for this information! Thanks!

  27. Hey there, I think your site might be having browser compatibility issues.
    When I look at your website in Ie, it looks fine but when opening
    in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, fantastic blog!

  28. Intuit Quicken Home & Business 2011 buy

    Hmm it looks like your blog ate my first comment (it was super long)
    so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any tips and hints for novice blog writers? I’d
    really appreciate it.

  29. I all the time used to read paragraph in news papers but
    now as I am a user of web so from now I am using net for articles, thanks to web.

  30. Ashampoo Burning Studio 16

    Link exchange is nothing else except it is just placing the other person’s
    web site link on your page at proper place and other person will
    also do same in favor of you.

  31. Post writing is also a fun, if you be familiar with after that you can write otherwise it is difficult to write.

  32. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I desire to suggest you few interesting things or suggestions.

    Maybe you can write next articles referring to this article.

    I want to read more things about it!

  33. Hey just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Firefox.

    I’m not sure if this is a format issue or something to do with browser compatibility but I figured I’d post to let
    you know. The design and style look great though!
    Hope you get the problem fixed soon. Cheers

  34. Woah! I’m really digging the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appearance.
    I must say you have done a superb job with this. Also, the blog loads extremely fast for me on Chrome.
    Excellent Blog!

  35. Pretty nice post. I simply stumbled upon your blog and wished to
    say that I have truly enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your feed and I am hoping you write again very soon!

  36. After I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any method you can remove me from that service? Thanks!

  37. adidas ultra boost 3.0

    An fascinating discussion is value comment. I think that it is best to write more on this topic, it won’t be a taboo topic however typically people are not enough to speak on such topics. To the next. Cheers

  38. I am typically to running a blog and i really admire your content. The article has actually peaks my interest. I am going to bookmark your site and maintain checking for brand spanking new information.

  39. Michael Kors Jacquard Monogram Large Coffee Satchels

    Thank you for all of your effort on this website. My mum really likes working on investigation and it is obvious why. My spouse and i learn all relating to the compelling way you give sensible tips and tricks via this web site and as well as cause response from visitors on this topic then our favorite daughter is always studying a great deal. Take pleasure in the remaining portion of the year. You’re carrying out a dazzling job.

  40. Adidas NMD Runner PK Custom Navy

    Thank you for your entire effort on this website. My mum really loves getting into internet research and it’s really obvious why. Most people know all concerning the dynamic means you provide good thoughts by means of your website and cause response from other individuals on this subject matter and our own daughter is in fact studying a lot. Take pleasure in the remaining portion of the new year. You’re the one performing a really great job.

  41. yeezy boost 350

    You must take part in a contest for probably the greatest blogs on the web. I will advocate this site!

  42. Good post. I be taught something more difficult on totally different blogs everyday. It will always be stimulating to learn content material from other writers and follow slightly something from their store. I抎 favor to make use of some with the content material on my weblog whether you don抰 mind. Natually I抣l give you a hyperlink on your internet blog. Thanks for sharing.

  43. Once I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a remark is added I get 4 emails with the identical comment. Is there any manner you may remove me from that service? Thanks!

  44. That is the suitable blog for anybody who desires to seek out out about this topic. You notice so much its almost laborious to argue with you (not that I really would want匟aHa). You undoubtedly put a new spin on a subject thats been written about for years. Great stuff, just great!

  45. A powerful share, I just given this onto a colleague who was doing a little evaluation on this. And he the truth is bought me breakfast because I discovered it for him.. smile. So let me reword that: Thnx for the deal with! But yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love reading more on this topic. If doable, as you become experience, would you thoughts updating your blog with extra details? It is highly helpful for me. Massive thumb up for this blog post!

  46. adidas ultra boost 3.0

    Hi there! I just want to give an enormous thumbs up for the good info you will have here on this post. I will likely be coming again to your weblog for more soon.

  47. There are certainly a whole lot of details like that to take into consideration. That may be a nice point to carry up. I provide the ideas above as general inspiration but clearly there are questions just like the one you carry up where the most important thing can be working in honest good faith. I don?t know if greatest practices have emerged round things like that, but I’m sure that your job is clearly recognized as a fair game. Both girls and boys really feel the impact of just a second抯 pleasure, for the remainder of their lives.

  48. Adidas Originals NMD Runner Primeknit Sneakers Grey

    I not to mention my pals were viewing the best advice from your web blog and immediately I got a horrible feeling I never expressed respect to the web site owner for those secrets. All the young men came as a consequence joyful to study them and have definitely been taking pleasure in them. Appreciate your being really thoughtful and also for settling on these kinds of fine information most people are really wanting to know about. My very own honest apologies for not saying thanks to you earlier.

  49. I am just writing to make you understand what a incredible encounter my wife’s child obtained viewing your webblog. She figured out lots of issues, including what it is like to have an incredible coaching nature to get most people without difficulty thoroughly grasp a number of extremely tough subject matter. You actually exceeded people’s expected results. Many thanks for providing such powerful, healthy, informative and even unique tips on your topic to Mary.

  50. yeezy boost 350

    I抦 impressed, I must say. Really rarely do I encounter a weblog that抯 each educative and entertaining, and let me tell you, you’ve gotten hit the nail on the head. Your thought is outstanding; the problem is something that not enough persons are talking intelligently about. I’m very completely satisfied that I stumbled across this in my search for something relating to this.

  51. There may be noticeably a bundle to know about this. I assume you made sure nice points in features also.

  52. yeezy boost 350

    I was very pleased to search out this net-site.I needed to thanks on your time for this excellent read!! I undoubtedly enjoying every little little bit of it and I’ve you bookmarked to check out new stuff you weblog post.

  53. It is really a nice and useful piece of info. I am glad that you shared this
    useful information with us. Please stay us up to date like this.
    Thank you for sharing.

  54. You actually make it seem so easy with your presentation but I find this matter
    to be really something that I think I would
    never understand. It seems too complicated and extremely broad
    for me. I’m looking forward for your next post, I will try to get the
    hang of it!

  55. After research a couple of of the weblog posts in your web site now, and I truly like your approach of blogging. I bookmarked it to my bookmark website list and can be checking back soon. Pls take a look at my site as well and let me know what you think.

  56. Having read this I thought it was rather enlightening. I appreciate you finding the time and effort to put this article together.
    I once again find myself spending way too much time both reading and commenting.
    But so what, it was still worth it!

  57. There are some interesting closing dates on this article but I don抰 know if I see all of them middle to heart. There is some validity but I will take maintain opinion until I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as effectively

  58. Hi there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly?
    My site looks weird when viewing from my apple iphone. I’m trying to find
    a theme or plugin that might be able to fix this issue.

    If you have any recommendations, please share. With thanks!

  59. Hiya! I just would like to give a huge thumbs up for the good information you’ve here on this post. I might be coming again to your blog for extra soon.

  60. Hey! I just wanted to ask if you ever have any issues with hackers?

    My last blog (wordpress) was hacked and I ended up losing several weeks of hard
    work due to no back up. Do you have any methods to prevent hackers?

  61. moncler jacket

    I intended to create you a very small remark to thank you very much once again regarding the fantastic suggestions you’ve documented on this page. It’s so tremendously open-handed of people like you to give easily what exactly most people could possibly have marketed for an e-book to help with making some profit on their own, precisely now that you might well have done it in the event you desired. The good ideas likewise worked to become good way to understand that other people online have the identical fervor really like my very own to learn whole lot more with regards to this issue. I know there are millions of more fun opportunities in the future for people who scan your site.

  62. I intended to send you one bit of word just to say thanks the moment again regarding the extraordinary solutions you have discussed here. It has been certainly surprisingly open-handed with you to grant openly what exactly a lot of people would have offered as an e book in making some dough for their own end, most notably now that you could have tried it if you considered necessary. Those concepts also worked like the fantastic way to recognize that someone else have a similar keenness like my own to know the truth many more in regard to this condition. I am sure there are many more pleasant sessions in the future for individuals who check out your blog post.

  63. I am also commenting to let you understand what a terrific discovery our daughter encountered viewing your site. She came to understand a lot of pieces, which include what it’s like to possess a marvelous teaching style to get certain people quite simply learn about some specialized subject matter. You really did more than her expectations. Thanks for showing the practical, trustworthy, educational and also easy tips about your topic to Tanya.

  64. I would like to get across my affection for your kindness for visitors who actually need help with the theme. Your personal commitment to passing the message all-around appeared to be extraordinarily significant and have frequently encouraged somebody just like me to get to their endeavors. Your personal valuable tips and hints imply a whole lot a person like me and much more to my fellow workers. Thanks a lot; from every one of us.
    nike huarache.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
1 Shares
Tweet
Pin1
Share
Share
Share