中的自定义wp搜索查询
$args = array(
'post_type' => 'post',
'taxonomy' => 'location',
'field' => 'name',
'posts_per_page' => 10,
'term' => $keyword,
);
$wp_query = new WP_Query($args);
我有2个帖子,其位置是伦敦和牛津,伦敦。现在我搜索伦敦然后它只显示1个帖子**(第1个)**。当我搜索牛津时,它显示没有找到结果。
当我搜索牛津伦敦时,它的工作正常。
答案 0 :(得分:2)
Befor WP_Query Run编写此代码
function advance_search_where($where){
global $wpdb;
if (is_search())
$where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
return $where;
}
function advance_search_join($join){
global $wpdb;
if (is_search())
$join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
return $join;
}
function advance_search_groupby($groupby){
global $wpdb;
// we need to group on post ID
$groupby_id = "{$wpdb->posts}.ID";
if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;
// groupby was empty, use ours
if(!strlen(trim($groupby))) return $groupby_id;
// wasn't empty, append ours
return $groupby.", ".$groupby_id;
}
add_filter('posts_where','advance_search_where');
add_filter('posts_join', 'advance_search_join');
add_filter('posts_groupby', 'advance_search_groupby');
现在运行你的WP_Query像这样
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
's' => $keyword,
);
$wp_query = new WP_Query($args);
在您完成所有结果之后,如果您不删除它,则必须删除上面的过滤器,然后它将应用于该页面的其他wp_query。
remove_filter('posts_where','advance_search_where');
remove_filter('posts_join', 'advance_search_join');
remove_filter('posts_groupby', 'advance_search_groupby');