# Send Searches That Match a Category to It

> Takes shoppers straight to a product category page when what they search for is exactly a category name, such as "dresses" or "garden tools".

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, search, categories, redirects
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-search-matches-category/

## Code

```php
add_action( 'template_redirect', function () {
    if ( ! is_search() || 'product' !== get_query_var( 'post_type' ) || is_paged() ) {
        return;
    }

    $term = get_term_by( 'name', trim( get_search_query( false ) ), 'product_cat' );

    if ( ! $term ) {
        $term = get_term_by( 'slug', sanitize_title( get_search_query( false ) ), 'product_cat' );
    }

    if ( $term && ! is_wp_error( $term ) ) {
        $link = get_term_link( $term );

        if ( ! is_wp_error( $link ) ) {
            wp_safe_redirect( $link );
            exit;
        }
    }
} );
```

## Notes

Matching ignores upper and lower case, and partial matches still show the normal search results.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Searches That Match a Category to It" 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/
