admin管理员组文章数量:1122846
hi i need your help regarding for woo-commerce product title based search we have list of product my searching keyword available in product title need to show list product. if product title don't have my search keyword but available in product description section this don't need to display this product.
hi i need your help regarding for woo-commerce product title based search we have list of product my searching keyword available in product title need to show list product. if product title don't have my search keyword but available in product description section this don't need to display this product.
Share Improve this question asked Dec 24, 2017 at 16:36 muprabakaranmuprabakaran 11 silver badge2 bronze badges2 Answers
Reset to default 0add below code on function.php file in your theme directory .
<?php
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 );
?>
If anybody needs to search by title only, but to keep the default "-" (hyphen) exclude word functionality, here's a slightly updated version:
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 ) );
// Allow words starting with the '-' sign to be excluded from the search!
if (str_starts_with($term, '-')) {
$term = substr($term, 1);
$search .= "{$searchand}($wpdb->posts.post_title NOT LIKE '{$n}{$term}{$n}')";
} else {
$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;}
本文标签: pluginswoocommerce product search based only title
版权声明:本文标题:plugins - woocommerce product search based only title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289468a1928311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论