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'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

1 Answer 1

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

 
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.