0

My categories structure :

  • Category 1 (8 Posts )

    • subcategory1 (2 Posts)
    • subcategory2 (2 Posts)
    • subcategory3 (2 Posts)
  • Category 2 (6 Posts)

    • subcategory1 (2 Posts)
    • subcategory2 (2 Posts)
    • subcategory3 (2 Posts)

I would like to query posts only from the Category 1 (8 posts) and Category 2 (6 Posts) without displaying posts from subcategories.

Here's the code sample :

<?php query_posts( array ( 'category_name' => $cat_slug, 'posts_per_page' => -1 ) );
                $number = 0; ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <?php 
            $title = the_title('','',false);
            $excerpt = get_the_excerpt();
            echo $title;
            echo $excerpt;
        ?>

<?php endwhile; else: endif; ?>

How can I edit above code to display posts only from categories 1 ,2 , without displaying posts from subcategories ??

1 Answer 1

0

You can use a tax query with include_children set to false:

$query = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => 'your-category-slug',
            'include_children' => false,
        ),
    ),
) );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.