Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the standard wordpress loop that looks like:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; else: ?>
        <h2>We're sorry...there is no content.</h2>
    <?php endif; ?>

Then, what I wanted to do was exclude a certain category from my query, so my code became:

    <?php 
        $parent = get_cat_ID("Sports");
        query_posts("cat=-".$parent); 
    ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <h2><?php the_title(); ?></h2>
        <?php endwhile; else: ?>
            <h2>We're sorry...there is no content.</h2>
        <?php endif; ?>
    <?php wp_reset_query(); ?>

Which I thought was right on the money...

So I did some research and discovered this is only excluding the category, not the category and it's subcategories. Is there a way to exclude a specific category and all it's subcategories from the loop?

Thanks, Josh

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

I recommend you use the pre_get_posts filter or ditch query_posts and use WP Query.

That way you can easily use category__not_in (array) parameter and not mess any other loops up.

function exclude_category($query) {

// this requires term id instead of term name so change "20" to the "sport" id
// this assumes "sports" is in a category and not a custom taxonomy
$child_cats = (array) get_term_children('20', 'category');

//only effect main home page query 
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('category__not_in',array_merge(array('20'), $child_cats));
return $query;
}
}

add_filter('pre_get_posts', 'exclude_category');

ps. I did not test this but in theory it should work.

share|improve this answer
add comment

This should help you:

Get the children of the parent category

Read all the sub-categories of the parent, loop over the result to get the IDs of them all and then include all IDs in the query_posts() call.

For example (not tested):

$parent = get_cat_ID("Sports");
$kids = get_categories( array( 'parent' => $parent ) );
$a = array( $parent );
foreach ( $kids as $kid )
{
  $a[] = $kid->ID;
}

query_posts( "cat=-" . implode( ",", $a ) ); 
share|improve this answer
    
Steve, this gets the child categories no problem...I can even output them as a list, but that's not what I need. What I am looking for is something that will do that automatically, so later on if I add more child categories to the category I won't have to go back and manually exclude them from the loop. –  Josh Rodgers Dec 7 '12 at 20:40
    
Hi Josh, there should be no manual additions at all, I've updated my answer with a (untested) code example. –  Steve Claridge Dec 7 '12 at 22:32
    
I tried your code and it seemed like nothing happened, not sure why it didn't work...any ideas? –  Josh Rodgers Dec 10 '12 at 16:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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