Skip to content
›How to Auto-Redirect When a Wo...

How to Auto-Redirect When a WordPress Search Returns One Result

In This Guide
Quick answer
  • When a WordPress search finds just one post, you can send visitors straight to it instead of showing a results page with one item.
  • Add a short code snippet that runs on the template_redirect hook: if it’s a search with exactly one result, redirect to that post.
  • Use a code snippet plugin like WPCode rather than editing theme files.
  • Test it with a search term that matches only one post.
A search results page with a single result is an unnecessary extra click. Here’s how to skip it in WordPress.

How it works

WordPress runs the template_redirect action before loading a page. A snippet can check is_search() and whether the main query found exactly one post ($wp_query->post_count == 1), then redirect with wp_safe_redirect() to that post’s permalink.

Step 1: Install a snippet plugin

Install WPCode from WordPress.org. It keeps custom code safe when you change themes.

Step 2: Add the snippet

  • Add a new PHP snippet.
  • Hook a function to template_redirect.
  • Inside it, check is_search() and that exactly one post was found.
  • Redirect to get_permalink() of that post, then exit.
  • Set the snippet to run on the front end only and activate it.

Step 3: Test

  • Search for a term that matches one post: you should land on it.
  • Search for a common term: you should see the results page.

When not to use it

If your search includes products and pages, check that redirecting fits your visitors’ expectations.

FAQs

Sharing is caring

Popular on Bloggersutra Right Now!

Disclosure: Our content is reader-supported. If you click some of our links, we may earn a commission at no extra cost to you. See how Bloggersutra is funded. Here is our editorial process.

About the Editorial Staff

Editorial Staff at Bloggersutra is a team of WordPress writers led by Munish Siwatch, who has worked on website design and blogging since 2009. We write tutorials, reviews and guides that help beginners start, run and grow a WordPress website.

Join the Conversation

Leave a comment


Free WordPress Guides

Follow Bloggersutra for fresh WordPress tips.

WordPress Deals & Coupons

Save on hosting, themes, plugins and blogging tools.

Latest Posts

Start Your WordPress Blog

Follow our beginner guides, from choosing a host to publishing your first post.

Deals & Coupons

Save over 40% on your first year
Up to 84% off + 2 free months
Up to 88% off + free domain