How to display child posts in the parent category

You need to use a tax_query with WP_Query. With a tax_query, by default, child terms are included to the term being set. Here is the example-

$args = array(
      'post_type'      => 'post', // Your post type
      'orderby'        => 'meta_value_num', 
      'meta_key'       => 'rankk', 
      'order'          => 'DESC', 
      'posts_per_page' => 100,
      'tax_query' => array(
            array(
                'taxonomy'         => 'category', // Taxonomy name
                'field'            => 'term_id', // Means you'll use term id to determine your parent term.
                'include_children' => true,
                'operator'         => 'IN'
                'terms'            => 'YOUR PARENT CATEGORY ID', // Your term or category ID
            ),
        ),
);

$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
        the_title();
    }
    wp_reset_postdata();
}

Hope this above helps.

Leave a Comment