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'm trying to get all post except particular category this works fine but it cause the pagination button/link to render the same posts again on page 2, 3, 4 and so on not rendering the older post. Here is how I am trying to exclude a particular category

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

This excludes the category but prevents pagination buttons/link from showing older posts, instead it shows the same first 5 posts as on page 1.

I tried moving the category exclusion bit into the <?php if ( have_posts('cat=-22') ) : ?> line but this doesn't change anything.

I'm trying to do this on the index.php and archive.php

I've also tried excluding the category by its name and I get the same results.

share|improve this question
up vote 3 down vote accepted

I would filter pre_get_posts. Perhaps like so, though you may need to tweak the conditional a bit:

function wpse71508_filter_pre_get_posts( $query ) {
    if ( is_main_query() ) {
        if ( is_home() || is_archive() ) {
            $query->set( 'cat', '-22' );
    return $query;
add_action( 'pre_get_posts', 'wpse71508_filter_pre_get_posts' );

If you could clarify the context (rather than the template file) to which you want to apply this query modification, I can be more specific.

share|improve this answer
Thanks! This works perfectly :) I was trying to remove category B from the archive page but the 'cat=-22' was breaking the pagination – Grundizer Nov 4 '12 at 15:07

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.