Home > OS >  Wordpress - Get posts in custom parent taxonomy
Wordpress - Get posts in custom parent taxonomy

Time:11-15

I want to get only posts of current category, not of child categories My code:

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

This code returns all posts in parent and child categories

CodePudding user response:

You can use 'include_children' => false param. try the below code.

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