# Minimum Search Term Length

> Ignores searches shorter than a few characters, which return huge, useless result lists and put extra load on the database.

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

## Code

```php
// Minimum number of characters for a search.
$wpide_min_search_length = 3;

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_min_search_length ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
        return;
    }

    $term   = trim( (string) $query->get( 's' ) );
    $length = function_exists( 'mb_strlen' ) ? mb_strlen( $term ) : strlen( $term );

    if ( '' !== $term && $length < $wpide_min_search_length ) {
        $query->set( 'post__in', array( 0 ) );
    }
} );
```

## Notes

Short searches show the normal "nothing found" page.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Minimum Search Term Length" 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/
