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

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

How-to-Auto-Redirect
Loading...

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

Loading...

About the author

Pradeep Beniwal

85 Comments

Click here to post a comment

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

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

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

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

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

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

  • 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

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

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

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

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

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

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

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

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

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

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

  • 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 =)

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

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

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

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

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

  • 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

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

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

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

  • 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • 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

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

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

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

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

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

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

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

Sponsors

Advertisement

Loading...