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 have done this before but for some reason, it is not working and I can't figure out why. All I am trying to do is exclude some categories from the blog page. I thought this would be a simple matter. I have the index.php file open, and before my loop, I did this

 query_posts( $guery_string . '&cat=-6' )
 if (have_posts)......rest of loop here.

I have even tried adding the global $query_string; on top but nothing I do will get rid of category 6. Does this method no longer work in the latest version of wordpress?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Don't use query_posts(). Filter pre_get_posts instead:

function wpse65927_filter_pre_get_posts( $query ) {
    // If this is the blog posts index,
    // and if this is the main query,
    // exclude category 6
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-6' );
    }
}
add_action( 'pre_get_posts', 'wpse65927_filter_pre_get_posts' );

Put the above hook and callback in functions.php. Don't modify any of your template files, including index.php or home.php, at all.

share|improve this answer
    
I am a little confused on how this would work. I have a file called template-homepage as my homepage. and I have created a page and called it blog. in the settings, I set my site to a static homepage and gave the front page the template-homepage and set the blog page to blog. The blog page is pulling from the index.php file which I modified with a loop so that the blog will look how I need it to look. How would wordpress look at the blog page as home in your filter above? –  Jamie Sep 22 '12 at 6:14
    
That's a nice little function. It works well. Thank you. –  Jamie Sep 22 '12 at 6:25

What happens if you use just:

query_posts( 'cat=-6' );

?

You shouldn't need to re-pass the query string to the index.php template.

share|improve this answer
    
it doesn't work. I tried all those combinations. –  Jamie Sep 22 '12 at 6:11

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.