WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use ids to control the structure of the output of my categories. I need to exclude a category that is part of the parent category that is being queried.

What I am trying is :

<?php $the_query = new WP_Query('cat=14$cat=-45&showposts=20');
        $permalink = get_permalink( $post_id );
        while ($the_query->have_posts()) : $the_query->the_post();?>    
            <a href="<?php the_permalink() ?>" rel="bookmark" class="postTitleLink">
            <?php the_title(); ?></a>
        <?php wp_reset_query(); ?><?php endwhile; ?>

What would be the correct way to show everything in category id 14, except for category 45? I want to treat 45 differently and show all of its sub categories in a different way. To try and help describe what I want :

- #14
- #14
- #14
 -- #45
  --- #50
   ---- #60
  --- #51
- #14

instead of cat=- should I be using conditional statements instead .. !in_category ?

Thanks for any help!

share|improve this question
up vote 2 down vote accepted

First you should move wp_reset_query(); outside of the loop instead of resetting the query every post of the loop.

And as for your query you can use category__not_in

$args = array(
    'cat' =>  14,
    'category__not_in' => array('45'),
    'posts_per_page' => 20

$the_query = new WP_Query($args);
while ($the_query->have_posts()) : $the_query->the_post();?>    
            <a href="<?php the_permalink() ?>" rel="bookmark" class="postTitleLink">
            <?php the_title(); ?></a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

this will exclude all posts of category 45.

share|improve this answer

Your Answer


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.