1

I created a new category with the ID of 57, how do I exclude this category from the homepage/blog and then list the posts from this category on its own page?

this is what my index.php looks like

<?php
            $popular_count = 1;
            query_posts( array( 'orderby' => 'comment_count','posts_per_page' => 6 ) );
            if ( have_posts() ) : while ( have_posts() ) : the_post();
            ?>

so how to add that bit of code is a little confusing

2 Answers 2

3

Rather than performing a separate query, to exclude a category (or any other taxonomy) term, you can hook into pre_get_posts:

add_action('pre_get_posts', 'wpse41820_exclude_cat_from_front_page');
function wpse41820_exclude_cat_from_front_page( $query ){
    if( $query->is_main_query() && is_front_page() ){
        $tax_query = array(array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => array( 57 ),
            'operator' => 'NOT IN'
           ));

        $query->set('tax_query',$tax_query);
    }
    return $query;
}

To exclude a slug, alter the $tax_query accordingly:

        $tax_query = array(array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'term-slug' ),
            'operator' => 'NOT IN'
           ));
0
0

Use a custom query for both.

   <?php if (have_posts()) : 

        $args = array(
                          'orderby' => 'comment_count',
                      'posts_per_page' => 6,
                      'cat' => -57,
                    );

        $my_query = new WP_Query($args);

        while ($my_query->have_posts()) : $my_query->the_post(); ?>

The negative sign in front of the category denotes an exclusion.

I made the edit to work with the code you provided.

5
  • where do i place this?
    – Joe
    Feb 9, 2012 at 21:47
  • @Joe In your index.php, replacing the code you posted.
    – Kyle
    Feb 9, 2012 at 22:17
  • i tried that and it didnt exclude the category which is y im a little confused
    – Joe
    Feb 9, 2012 at 22:20
  • is there anything that could be affecting this from working? I put I replaced it correctly and its not working for some reason..
    – Joe
    Feb 9, 2012 at 22:26
  • @Joe I edited my answer again to use what I know works. Try replacing your code with my edit and see if it works.
    – Kyle
    Feb 9, 2012 at 23:03

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