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

How-to-Auto-Redirect

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

   Rate This Article To Help You Better

1 Star2 Stars3 Stars4 Stars5 Stars (Be The First One To Rate)

Loading...
https://bloggersutra.com

Hi, I am Pradeep Beniwal and I am into website designing and blogging from 2009 onwards. Finally, I have developed this blog to help everyone creating their own blog and websites. Articles will help to develop your own blog easily and also provides you tips to earn money from your blog.

    All Comments

    • 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.

      ballard13cramer.wikidot.com 1 Sep 2017 10:06 am Reply
    • 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. . . . . .

      Krista 3 Sep 2017 6:26 am Reply
    • Hello, I read your blogs on a regular basis. Your humoristic style
      is witty, keep up the good work!

      Les 12 Sep 2017 7:15 am Reply
    • 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?

      Kristan 14 Sep 2017 1:41 am Reply
    • 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.

      Elissa 15 Sep 2017 12:35 pm Reply
      • Please contact us via our contact page and provide details of your requirement.

        Pradeep Beniwal 18 Sep 2017 1:21 pm Reply
    • I every time emailed this weblog post page to all my friends, since if like to read it next my contacts willl too.

      Angel 16 Sep 2017 4:24 am Reply
    • Wayy cool! Some very valid points! I appreciae you penning this article plys the rest of the site is really
      good.

      Christy 16 Sep 2017 6:32 am Reply
    • I could not resist commenting. Perfectly written!

      Deanna 17 Sep 2017 12:33 pm Reply
    • 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.

      Rosemarie 19 Sep 2017 2:48 am Reply
    • Really no matter if somwone doesn’t know after
      that its up to other users tht they will assist, so
      here it takes place.

      Mckinley 6 Nov 2017 10:19 am Reply
    • I genuinely enjoy reading through on this internet site, it contains fantastic content.

      borvest inkral 8 Nov 2017 7:39 pm Reply
    • 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.

      viagra 22 Dec 2017 8:07 am Reply
    • 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

      cheap 22 Dec 2017 11:42 pm Reply
    • Thanks in favor of sharing such a fastidious opinion,
      article is pleasant, thats why i have read it fully

      kamagra 23 Dec 2017 5:55 am Reply
    • 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.

      source 23 Dec 2017 4:02 pm Reply
    • 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.

      source 23 Dec 2017 7:57 pm Reply
    • 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!

      levitra 23 Dec 2017 8:43 pm Reply
    • I think the admin of this website is really working hard
      for his web page, as here every information is quality based stuff.

      here 23 Dec 2017 10:59 pm Reply
    • 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.

      propecia 23 Dec 2017 11:05 pm Reply
    • 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.

      here 24 Dec 2017 1:15 am Reply
    • 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.

      here 24 Dec 2017 5:31 am Reply
    • 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.

      source 24 Dec 2017 5:35 am Reply
    • 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!

      source 24 Dec 2017 9:00 am Reply
    • This article is truly a fastidious one it helps new net visitors, who are wishing
      for blogging.

      here 24 Dec 2017 10:42 am Reply
    • Great delivery. Outstanding arguments. Keep up the good work.

      source 24 Dec 2017 10:45 am Reply
    • 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.

      source 24 Dec 2017 8:27 pm Reply
    • 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!

      source 24 Dec 2017 11:13 pm Reply
    • 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.

      here 25 Dec 2017 10:06 am Reply
    • 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 =)

      here 25 Dec 2017 5:16 pm Reply
    • 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!

      cheap 25 Dec 2017 9:21 pm Reply
    • 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!

      buy 25 Dec 2017 10:25 pm Reply
    • 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!

      cheap 26 Dec 2017 1:59 am Reply
    • 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.

      Intuit Quicken Home & Business 2011 buy 26 Dec 2017 2:00 am Reply
    • 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.

      source 26 Dec 2017 7:41 am Reply
    • 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.

      Ashampoo Burning Studio 16 26 Dec 2017 9:12 am Reply
    • Post writing is also a fun, if you be familiar with after that you can write otherwise it is difficult to write.

      source 26 Dec 2017 9:47 am Reply
    • 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!

      source 27 Dec 2017 11:27 pm Reply
    • 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

      source 29 Dec 2017 9:46 am Reply
    • 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!

      here 5 Jan 2018 12:06 pm Reply
    • 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!

      desywulandari 18 Jan 2018 8:32 pm Reply
    • 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!

      adidas yeezy 6 Mar 2018 11:21 am Reply
    • 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

      adidas ultra boost 3.0 7 Mar 2018 6:20 pm Reply
    • 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.

      yeezy boost 8 Mar 2018 5:57 pm Reply
    • 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.

      Michael Kors Jacquard Monogram Large Coffee Satchels 8 Mar 2018 7:44 pm Reply
    • Your home is valueble for me. Thanks!?

      adidas yeezy boost 9 Mar 2018 4:52 pm Reply
    • 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.

      Adidas NMD Runner PK Custom Navy 9 Mar 2018 8:01 pm Reply
    • You must take part in a contest for probably the greatest blogs on the web. I will advocate this site!

      yeezy boost 350 10 Mar 2018 5:19 pm Reply
    • 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.

      golden world 11 Mar 2018 11:12 am Reply
    • 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!

      golden world 12 Mar 2018 4:48 am Reply
    • Your home is valueble for me. Thanks!?

      golden world 12 Mar 2018 10:57 pm Reply
    • 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!

      adidas nmd r1 13 Mar 2018 6:19 am Reply
    • 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!

      golden world 13 Mar 2018 8:07 pm Reply
    • 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.

      adidas ultra boost 3.0 14 Mar 2018 4:51 pm Reply
    • 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.

      golden world 14 Mar 2018 5:08 pm Reply
    • 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.

      Adidas Originals NMD Runner Primeknit Sneakers Grey 15 Mar 2018 9:10 am Reply
    • 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.

      air max 90 15 Mar 2018 7:33 pm Reply
    • very nice post, i definitely love this website, keep on it

      golden world 16 Mar 2018 6:50 am Reply
    • 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.

      yeezy boost 350 17 Mar 2018 2:52 am Reply
    • There may be noticeably a bundle to know about this. I assume you made sure nice points in features also.

      nmd uk 18 Mar 2018 7:39 am Reply
    • 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.

      yeezy boost 350 19 Mar 2018 8:31 am Reply
    • 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.

      here 9 Apr 2018 9:08 pm Reply
    • 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!

      here 9 Apr 2018 11:39 pm Reply
    • 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.

      up to top 14 Apr 2018 12:07 pm Reply
    • 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!

      here 15 Apr 2018 3:08 am Reply
    • 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

      up to top 17 Apr 2018 10:25 pm Reply
    • 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!

      here 18 Apr 2018 3:35 am Reply
    • WONDERFUL Post.thanks for share..extra wait .. ?

      up to top 18 Apr 2018 7:46 pm Reply
    • 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.

      up to top 19 Apr 2018 9:01 pm Reply
    • 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?

      here 22 Apr 2018 10:41 pm Reply
    • 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.

      moncler jacket 23 Apr 2018 8:48 pm Reply
    • 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.

      d rose shoes 24 Apr 2018 9:00 pm Reply
    • 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.

      kobe shoes 26 Apr 2018 10:25 am Reply
    • 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.

      nike huarache 4 May 2018 12:56 pm Reply

    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.

    Alexa Ranking Booster : Get 15000 Free Visitors For Your Website
    close-image
    Share4
    Tweet
    +11
    Share5
    Pin1
    5 Shares