【问题标题】:I want to set "post_type OR tax_query"我想设置“post_type OR tax_query”
【发布时间】: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 吗?

【问题讨论】:

    标签: wordpress taxonomy


    【解决方案1】:

    在您的“tax_query”集旁边的类别循环之后设置“关系”

    $query->tax_query->relation ='OR';

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2019-06-14
      相关资源
      最近更新 更多