【问题标题】:WP_Query : if I combine 'p' AND 'tax_query' , 'tax_query' clause is being ignoredWP_Query :如果我结合 'p' AND 'tax_query' , 'tax_query' 子句将被忽略
【发布时间】:2020-12-03 10:59:36
【问题描述】:

如果我执行这个查询:

new WP_Query(array( 
            'post_status' => 'publish',
            'post_type' => 'page', 
            'tax_query' => array(
                        'relation' => 'OR',
                            array(
                                'taxonomy' => 'post_tag',
                                'field' => 'slug',
                                'terms' => 'type1',
                            ),array(
                                'taxonomy' => 'post_tag',
                                'field' => 'slug',
                                'terms' => 'type2',
                            )
                        ) 
            )
        );

返回一个空数组,没错

但如果我尝试使用子句“p”执行相同的查询,它会返回数据并忽略 tax_query 子句。

new WP_Query(array( 
            'post_status' => 'publish',
            'post_type' => 'page', 
            'p' => 300 ,
            'tax_query' => array(
                        'relation' => 'OR',
                            array(
                                'taxonomy' => 'post_tag',
                                'field' => 'slug',
                                'terms' => 'type1',
                            ),array(
                                'taxonomy' => 'post_tag',
                                'field' => 'slug',
                                'terms' => 'type2',
                            )
                        ) 
            )
        );

【问题讨论】:

  • 我认为您必须将'p' => 300 , 更改为'page_id' => 300, p 参数用于帖子ID,并且您正在查询页面
  • 如果我只使用“p”(或“page_id”)查询工作正常。另外,如果我只使用 tax_query

标签: php sql wordpress


【解决方案1】:

您似乎使用了不正确的帖子类型。您不能将 post_tag 用于页面帖子类型,因为它仅用于帖子分类。

new WP_Query(array( 
    'post_status' => 'publish',
    'post_type' => 'post', 
    'tax_query' => array(
        'relation' => 'OR',
        array(
            taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'type1',
        ),
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'type2',
        )
    ) 
));

【讨论】:

  • 我不同意,第一个查询就像一个魅力。
猜你喜欢
  • 2013-01-20
  • 2012-06-12
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 2014-09-14
  • 2021-03-22
  • 2014-03-06
相关资源
最近更新 更多