方法非常简单,只需要下面代码就可:
add_action('posts_search', function($search, $query){
global $wpdb;
if ($query->is_main_query() && !empty($query->query['s'])) {
$sql = " OR EXISTS (SELECT * FROM {$wpdb->postmeta} WHERE post_id={$wpdb->posts}.ID and meta_key = 'product_no' and meta_value like %s)";
$like = '%' . $wpdb->esc_like($query->query['s']) . '%';
$search .= $wpdb->prepare($sql, $like);
}
return $search;
},2,2);
只需要将上面的商家编码的字段“product_no”替换成你所需搜索的相关字段即可。