Minimum Search Term Length
Ignores searches shorter than a few characters, which return huge, useless result lists and put extra load on the database.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.