【问题标题】:WP query to get posts from category ( not child of it )WP 查询从类别中获取帖子(不是它的子类别)
【发布时间】:2016-05-24 14:21:54
【问题描述】:

我有这个用于 WP_Query 的 $args:

$args = array(
        'post_type' => 'estructura',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => '12',
            ),
        )
    );

Witch 返回标记为类别 12 或标记为子类别(共 12 个)的帖子

我只想要标记为 12 类的那些,

如何防止孩子退回?

谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我能够使用include_children 选项解决这个问题

    $args = array(
        'post_type' => 'estructura',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    =>  $tax_id, // 12
                'include_children' => false
            )
        )
    );
    

    【讨论】:

      【解决方案2】:

      当您使用tax_query 时,它会获取所有孩子,因为他们的网址中有父母的网址(例如 website.com/category_name/child_1/)。

      $query = new WP_Query( array( 'cat' => 12 ) );
      

      这应该可以正常工作。来源:https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-08
        • 2014-07-12
        • 1970-01-01
        • 2019-10-24
        • 2018-06-29
        • 2011-08-29
        • 2018-09-22
        • 1970-01-01
        相关资源
        最近更新 更多