Modifying the woocommerce search to search for product title’s only
-
Upon analyzing the searh through query monitor plugin, I can see that post_excerpt and post_content are also considered aswell as various statuses. See below query for full extract.
SELECT SQL_CALC_FOUND_ROWS wpio_posts.ID FROM wpio_posts WHERE 1=1 AND ( wpio_posts.ID NOT IN ( SELECT object_id FROM wpio_term_relationships WHERE term_taxonomy_id IN (6) ) ) AND (((wpio_posts.post_title LIKE '%iphone%') OR (wpio_posts.post_excerpt LIKE '%iphone%') OR (wpio_posts.post_content LIKE '%iphone%'))) AND wpio_posts.post_type = 'product' AND (wpio_posts.post_status = 'publish' OR wpio_posts.post_status = 'xlwctydisabled' OR wpio_posts.post_status = 'open' OR wpio_posts.post_status = 'closed' OR wpio_posts.post_status = 'private') GROUP BY wpio_posts.ID ORDER BY wpio_posts.post_title LIKE '%iphone%' DESC, wpio_posts.post_date DESC LIMIT 0, 30
There was a suggestion from an article (https://www.isitwp.com/limit-search-to-post-titles-only/) which involved adding this code in the theme’s function.php file.
function __search_by_title_only( $search, &$wp_query ) { global $wpdb; if ( empty( $search ) ) return $search; // skip processing - no search term in query $q = $wp_query->query_vars; $n = ! empty( $q['exact'] ) ? '' : '%'; $search = $searchand = ''; foreach ( (array) $q['search_terms'] as $term ) { $term = esc_sql( like_escape( $term ) ); $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')"; $searchand = ' AND '; } if ( ! empty( $search ) ) { $search = " AND ({$search}) "; if ( ! is_user_logged_in() ) $search .= " AND ($wpdb->posts.post_password = '') "; } return $search; } add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
After implementing this, i analyzed search again and it appears that it has no affect on search and query is still happening the same as before.
I want to complete this task without any use of search plugins as they have there own issues and slow down the site.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Modifying the woocommerce search to search for product title’s only’ is closed to new replies.