Easiest way to exclude parent cat but not children WordPress

Is there any way to include posts that are in any child of category 982, but exclude those which are only in category 982?

<?php query_posts($query_string . '&cat=-282,-428,,-1046,-1103,-982'); ?>

Related posts

Leave a Reply

1 comment

  1. I’d first start by getting the children ID’s of the parent category, storing them in a list, and then passing them in as a parameter as an include.

    <?php
    
    $cats = wp_list_categories('echo=0&title_li=&child_of=982');
    $args = array(
        'cat' => array(
            '-282', 
            '-428',
            '-1046',
            '-1103',
            '-982'
        )
    );
    
    foreach($cats as $cat) {
        $args['cat'][] = $cat->id;
    } 
    
    $posts = query_posts( $args );
    

    Something along those lines.


    You can also take the non-associative array approach.

    <?php
    
    $cats = wp_list_categories('echo=0&title_li=&child_of=982');
    
    $include_cats = "";
    
    if(!empty($cats)) {
        foreach($cats as $cat) {
            $include_cats .= "," . $cat->id;
        } 
    }
    
    $posts = query_posts($query_string . '&cat=-282,-428,-1046,-1103,-982' . $include_cats );