【问题标题】:Wordpress - Get posts in custom parent taxonomyWordpress - 在自定义父分类中获取帖子
【发布时间】:2021-11-14 14:27:18
【问题描述】:

我只想获取当前类别的帖子,而不是子类别的帖子 我的代码:

$cat = get_queried_object(); // Current category

$args = array(
        'posts_per_page' => -1,
        'post_type' => 'products',
        'orderby' => 'title',
        'order'   => 'ASC',
        
        'tax_query' => array(
          array(
            'taxonomy' => 'section',
            'field' => 'term_id',
            'terms' => $cat->term_id,
          )
        )
                 
    );



    $products = get_posts($args);

此代码返回父子类别中的所有帖子

【问题讨论】:

    标签: wordpress custom-taxonomy


    【解决方案1】:

    您可以使用'include_children' => false 参数。试试下面的代码。

    $cat = get_queried_object(); // Current category
    
    $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'products',
        'orderby'        => 'title',
        'order'          => 'ASC',
        'tax_query'      => array(
            array(
                'taxonomy'         => 'section',
                'field'            => 'term_id',
                'terms'            => $cat->term_id,
                'include_children' => false
            )
        )
                 
    );
    
    $products = get_posts( $args );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-28
      • 2021-12-15
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 2017-06-02
      相关资源
      最近更新 更多