将自定义字段添加到wordpress搜索检索范围中

暂无评论 本文标签:
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”替换成你所需的相关字段即可。