下面是一个使用posts_clauses
过滤器:
function search_posts_clauses($clauses, $query){
if(!is_admin() && $query->is_main_query() && $query->is_search()){
global $wpdb;
// replace post_content with meta_key and meta_value:
$searchterms = explode(" ",get_query_var("s"));
foreach($searchterms as $searchterm){
$from = sprintf("{$wpdb->posts}.post_content LIKE \'%%%s%%\'", $searchterm);
$to = sprintf("{$wpdb->postmeta}.meta_key = \'cities\' AND {$wpdb->postmeta}.meta_value = \'%s\' ", $searchterm);
$clauses["where"] = str_replace($from, $to, $clauses["where"]);
}
$clauses["join"] = "INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) ";
}
return $clauses;
}
add_filter( \'posts_clauses\', \'search_posts_clauses\', 10, 2 );
我们在其中修改
where
和
join
主搜索查询的部件。