Support » Fixing WordPress » Exclude posts by category on index page

  • I am sorry to post this but I have gone blind looking at it. I have entered this in my main index page

    if (have_posts()) : while (have_posts()) : the_post();

    // The following determines what the post format is and shows the correct file accordingly

    Simple or so you would think. It does not work and I don’t know why. I have tried


    But that does not work either. The same code in a template file works fine. Can anyone stop me going mad!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Depending on what you’re trying to accomplish, query_posts() may not be the best- or preferred- way to get it done. But assuming it is, see below. Also, be sure that the category ID you’re trying to exclude is actually “51” – if it isn’t, this won’t do you any good…

    $args = array(
           'cat' => '-51',
           'posts_per_page' => 10,
           'order' => 'DESC'

    …Then your loop. After your loop, you’ll probably want to reset the query:

    endwhile; // End of the loop
    // Reset

    If you want to display every post in your blog, you can set ‘posts_per_page’ to ‘-1’. Then depending on your theme, you can also paginate the output with ‘paged’ => $paged.


    Can I ask if you’re modifying the main Loop in index.php or are trying to create a secondary query & Loop?

    The main loop in the query page


    Thanks for that. Not sure I see the material difference but I have tried it and it makes no difference. My page should not show the QR post as it is cat 51 but it does. My page (template page but using same code loop) uses the same statement ‘cat=51’ and works fine.

    an alternative way is to use ‘pre_get_posts’; whose Codex chapter has a matching example:

    this code goes into functions.php of the theme;

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-51' );
    add_action( 'pre_get_posts', 'exclude_category' );

    Thanks for the function and its a little more elegant for future use but does not solve the issue. Cat 51 is still appearing? I have checked again and again to make sure it is the right cat etc but as I say it works else where.

    have you verified that the ‘blog’ page is done by the main index.php template?

    a similar approach to your first idea:

    before the start of the loop, add:

    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'cat' => '-51' ) );
    query_posts( $args );


    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'category__not_in' => array( 51 ) ) );
    query_posts( $args );

    Yes I put a little tracking text in ‘Hi your on the right page’ just before I started to pull my hair out.

    Tried including he global statement no difference.

    I still cannot resolve this

    if (have_posts()) : while (have_posts()) : the_post();

    // The following determines what the post format is and shows the correct file accordingly’

    When I have cat=51 in the line only blogs of cat 51 appear when I have (minus)-51 all blogs appear including 51?

    Anyone any other suggestions?

    I have cracked it!

    It is not in the code. The original blog was posted with Cat=43 that’s was then changed to cat=51 (and only 51). The data base still has an entry for the previous version of the post as it would, but the query_posts() seems to return 43 and 51 which explains why it appears.

    I have written a rather poor sql script which returned both but I had assumed that query_posts would have been more sophisticated! Does this count as a bug?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Exclude posts by category on index page’ is closed to new replies.