# Go Straight to a Single Search Result

> When a search returns exactly one result, opens that result directly instead of showing a results page.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, redirect, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-single-search-result/

## Code

```php
add_action( 'template_redirect', function () {
    global $wp_query;

    if ( is_search() && ! is_paged() && 1 === (int) $wp_query->found_posts && ! empty( $wp_query->posts ) ) {
        wp_safe_redirect( get_permalink( $wp_query->posts[0] ) );
        exit;
    }
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Go Straight to a Single Search Result" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
