admin管理员组文章数量:1122832
I'm facing a challenge with a WordPress project and I would appreciate the community's help in solving it.
The issue lies with the search functionality. When customers type in a search term, such as "tooth," WordPress returns results that contain words like "toothmed" and "yellowtooth." However, I would like the search to be more precise and return only results with the exact term entered, not words that contain the term as part of them.
I believe this can be addressed using a hook that handles search results before they are sent to the database. I need this hook to remove wildcard characters (%) before and after the search terms, ensuring that results are displayed only when the searched term is bounded by word boundaries.
I appreciate any help or guidance you can provide to solve this issue. If anyone has experience with hooks or has encountered a similar situation, please share your expertise.
Thank you in advance for your cooperation!
I'm facing a challenge with a WordPress project and I would appreciate the community's help in solving it.
The issue lies with the search functionality. When customers type in a search term, such as "tooth," WordPress returns results that contain words like "toothmed" and "yellowtooth." However, I would like the search to be more precise and return only results with the exact term entered, not words that contain the term as part of them.
I believe this can be addressed using a hook that handles search results before they are sent to the database. I need this hook to remove wildcard characters (%) before and after the search terms, ensuring that results are displayed only when the searched term is bounded by word boundaries.
I appreciate any help or guidance you can provide to solve this issue. If anyone has experience with hooks or has encountered a similar situation, please share your expertise.
Thank you in advance for your cooperation!
Share Improve this question edited May 7, 2024 at 15:47 vancoder 7,91427 silver badges35 bronze badges asked May 4, 2024 at 21:45 Roniery RêgoRoniery Rêgo 11 bronze badge 2- "REMEMBERING THAT IF I SEARCH FOR 'TOOTH' AND IT RETURNS 'YELLOW TOOTH' 'TOOTH YELLOW' THOSE ARE DESIRED RESULTS, BECAUSE THE TERMS ARE SEPARATED AND AS IT FOUND A RESULT FOR THE TERMS THIS SCENARIO SATISFIES ME." – Roniery Rêgo Commented May 4, 2024 at 21:52
- Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Bot Commented May 5, 2024 at 16:54
1 Answer
Reset to default 0You could try something like this below. Basically, if we are performing a search and it is the main_query (the query that generates the page results) ignore the default search and perform an exact match via the query_where
function custom_product_search_exact_match( $query ) {
global $wpdb;
if ( is_search() && is_main_query() && !empty( $query->query_vars['s'] ) ) {
$search_term = $query->query_vars['s'];
$query->query_vars['s'] = '';
$query->query_where .= " AND ($wpdb->posts.post_title = '$search_term')";
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_product_search_exact_match' );
本文标签: wp queryModify WordPress Search
版权声明:本文标题:wp query - Modify WordPress Search 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307951a1933489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论