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

  • A+
所属分类: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”替换成你所需的相关字段即可。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: