Remove Specific Categories From The Loop

<?php query_posts('cat=-3'); ?>

<?php if (have_posts()) : ?>

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

		<p><?php the_time('F jS, Y') ?></p>

		<?php the_content(); ?>

<?php endwhile; ?>

The area inside the while loop could be anything, that's just some example typical inside-loop code. The trick is the first line, and the cat=-3 parameter. "-3" in this case is the ID of the category, and you can replace that with any category you wish to remove from the loop. You may use comma separated values here to remove more than one category (e.g. cat=-1,-2).


    • User Avatar
      Permalink to comment#


      Do you know how to code this if you used /work/ instead of an id.


  1. User Avatar
    Permalink to comment#

    i use it within looping. is it ok?

    • User Avatar
      Permalink to comment#


       <?php if (have_posts()) : ?>
       <?php query_posts($query_string.'&cat=-70'); while (have_posts()) : the_post(); ?>
      - - -  content - - -
      <?php endwhile; ?>
      <?php wp_pagenavi(); ?>
      <?php else : ?>
      <?php include(TEMPLATEPATH . '/404.php'); ?>
      <?php endif; ?>

      because i’m using wp_pagenavi… is it ok?

  2. User Avatar

    Messed with pagination, you guys should check some things before starting giving tips.

  3. User Avatar
    Permalink to comment#
                <?php twentyeleven_content_nav( 'nav-above' ); ?>
                <?php /* Start the Loop */ ?>
                <?php while ( have_posts()) : the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
                <?php twentyeleven_content_nav( 'nav-below' ); ?>
            <?php else : ?>
                <article id="post-0" class="post no-results not-found">
                    <header class="entry-header">
                        <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
                    </header><!-- .entry-header -->
                    <div class="entry-content">
                        <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
                        <?php get_search_form(); ?>
                    </div><!-- .entry-content -->
                </article><!-- #post-0 -->
            <?php endif; ?>

    Something goes wrong with the pagination. As in it hides the category specified but certain non-specified categories don’t show up. Seems, that the solution provided by you guys messes up the pagination code. Please Help !!!

  4. User Avatar
    Permalink to comment#

    Uhh, not sure what happened, but I meant to post that even though this post is a little old, people who come across it today might want to take care in using $query_posts() since it can be a very risky function. I’d suggest using WP_Query() instead.

    Here’s a sample from a project I’m working on now (which…uhm, you got a little sample of above…)

            <ol class="posts-list">
                <?php // exclude posts in gallery category
                    $catid = get_cat_ID('gallery');
                    $args = array('cat' => '-' . $catid);
                    $the_query = new WP_Query( $args );
                    if ( $the_query->have_posts() ) : ?>
                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                <h1 class="entry-title">
                                    <a href="<?php the_permalink(); ?>" title="<?php echo get_the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                <span class="entry-date"><?php echo get_the_date(); ?></span>
                                <span class="post-tags"><?php echo the_tags("",",",""); ?></span>
                    <?php endwhile; ?>
                <?php endif; ?>
    • User Avatar
      Permalink to comment#

      Brilliant, this is exactly what I was looking for. Thank you Aaron.

  5. User Avatar
    Permalink to comment#


    global $query_string;
    query_posts($query_string . '&cat=-45, -46');
    while ( have_posts() ) : the_post()
    • User Avatar
      jayshankar krish
      Permalink to comment#

      Thank You victor, your code has come handy for one of my website. Thanks a lot!!

  6. User Avatar
    Permalink to comment#

    Excluding specific categories in certain pages seems to be more tricky.
    This code below for exmaple does not work. Any idea what to do if I want to show certain categories on certain pages (= excluding the rest)?

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.