Redirect WordPress Search to the Only Result Found

When a WordPress search finds only one result, the visitor still lands on a results page and has to click once more. In a shop where people search for an exact model number, that extra page is useless.

This snippet redirects a WordPress search with exactly one result straight to that post, page or product.

The snippet

<?php
/**
 * Send the visitor straight to the result when a search finds exactly one.
 */
add_action( 'template_redirect', 'dpc_redirect_single_search_result' );

function dpc_redirect_single_search_result() {
	global $wp_query;

	$url = dpc_single_search_result_url( $wp_query );
	if ( '' !== $url && wp_safe_redirect( $url, 302, 'dpc-single-search-result' ) ) {
		exit;
	}
}

function dpc_single_search_result_url( $query ) {
	if ( ! $query instanceof WP_Query || ! $query->is_search() || $query->is_feed() || $query->is_paged() ) {
		return '';
	}
	if ( 1 !== (int) $query->found_posts || empty( $query->posts ) ) {
		return '';
	}

	return (string) get_permalink( $query->posts[0] );
}

How it works

  • template_redirect runs on the front end after WordPress has run the main query and before the template loads, so the search results are known and a redirect is still possible.
  • It checks found_posts, the total number of matches. The old version checked post_count, the number of posts on the current page, which is also 1 on the last page of a longer result list.
  • It does nothing on page 2 and later, in search feeds, and on any request that is not a search.
  • wp_safe_redirect() only redirects to your own site, and exit stops WordPress from rendering the results page after the redirect header has been sent. The old version had no exit.
  • The redirect is a 302, so browsers and search engines do not remember it: the same search can return more results tomorrow.

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. The redirect runs on template_redirect, which fires on every front-end request after the code has loaded.

The same code also works unchanged in a small plugin or an mu-plugin.

Customise it

  • Only for products: add || 'product' !== get_post_type( $query->posts[0] ) to the second check, so the search still shows a page with one blog post.
  • WooCommerce product searches (?s=x100&post_type=product) work the same way, because they are main search queries too.
  • To restrict your search first, see Limit WP Search to only Post Title and Limit search to post type.

Gotchas

  • Live search plugins that load results with AJAX do not go through template_redirect, so they are not redirected.
  • Visitors who want to see the search term highlighted in the results never see that page. That is the point, but keep it in mind if you track searches with an analytics tool on the results page.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.