Match Product Searches on Titles Only
Makes product searches look only at product names instead of full descriptions, giving shorter and more relevant results for catalogues with long descriptions.
Opens WPIDE on
Code
add_filter( 'posts_search', function ( $search, $query ) {
global $wpdb;
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || 'product' !== $query->get( 'post_type' ) ) {
return $search;
}
$terms = (array) $query->get( 'search_terms' );
if ( ! $terms ) {
return $search;
}
$clauses = array();
foreach ( $terms as $term ) {
$clauses[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like( $term ) . '%' );
}
return ' AND (' . implode( ' AND ', $clauses ) . ') ';
}, 20, 2 );
Notes
Only product searches on the front end are affected; blog searches keep matching content.
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.