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.

1 Answer 1


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.

  • Thanks! This works perfectly :) I was trying to remove category B from the archive page but the 'cat=-22' was breaking the pagination
    – zadubz
    Nov 4, 2012 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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