Wordpress - 获取自定义父分类中的帖子

Wordpress - Get posts in custom parent taxonomy

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

$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);

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

您可以使用 '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 );