WooCommerce通过使用wordpress默认搜索来搜索自定义产品属性

时间:2019-03-12 11:40:42

标签: php html wordpress search woocommerce

我正在为woocommerce制作一个自定义搜索表单,并且有两个自定义产品属性:

  • car_model
  • car_brand

如何使wordpress默认搜索表单根据产品标题和自定义产品属性car_modelcar_brand进行搜索?。

这是我的HTML输出代码:

    <label class="field_label" for="name_search1">Search by Name:</label>
    <input type="search" id="name_search1" class="search-field" placeholder="Put the car…" value="">
    <button class="home_search_button" type="submit" value="Search">Search</button>

我发现这些代码将搜索范围限制为仅来自here的标题。有没有机会通过从那里更改一些代码来解决问题。

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 );

0 个答案:

没有答案