【发布时间】:2016-08-26 16:03:28
【问题描述】:
我实际上在 pre_get_post 上有这样的钩子:
add_action('pre_get_posts', 'my_post_pre_get_posts');
function my_post_pre_get_posts($query)
{
if (!is_admin() && $query->is_main_query() && $query->is_home()) {
$query->set('posts_per_page', 3);
//FILTERS
$type = array();
if(!empty($_GET['type'])) {
$type = explode(',', $_GET['type']);
}else {
$type = array('post', 'testimonial', 'project');
}
$query->set('post_type', $type);
//CATEGORIES
if(!empty($_GET['category'])) {
$category = explode(',', $_GET['category']);
$args = array(
'relation' => 'OR',
);
foreach ($category as $categorie) {
array_push($args,
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
)
);
}
$query->set('tax_query', $args);
}
}
}
这样,我的主页会显示帖子以及另外两个自定义帖子类型:“项目”和“推荐”。
我的帖子有类别(但项目和推荐没有),我想用它过滤它们。但我不希望当我使用分类过滤时隐藏项目和推荐结果。
所以我想要一个“OR”关系,而不是 post_type 和分类法之间的“AND”关系。你认为有可能吗?我必须重写全局 $wpdb 吗?
【问题讨论】: