WordPress.org

Support

Support » How-To and Troubleshooting » excluding posts in a category with query_posts

excluding posts in a category with query_posts

  • I’ve been trying to display posts from a single category in a parent category template, but for some reason, the child posts I’m trying to exclude aren’t being excluded. My category template is called category-work (the name of the parent category, id 1) and I want to only display posts from the child category 4, but exclude posts from child categories 5, 6 and 7. So, posts in categories 5 and 6 don’t display, but the post in category 7 does. I can’t imagine what could be going wrong…

    This is my code.

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 5 replies - 1 through 5 (of 5 total)
  • OK, didn’t know about that moderation thing, which probably explains why there’s been no response to my question. Here’s a shortened version…

    <?php query_posts( 'orderby=rand&posts_per_page=1&cat=4,-5,-6,-7' ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>"></a>
    <?php endwhile; ?><?php endif; ?>
    <?php wp_reset_query(); ?>

    Again, the posts in categories 5 and 6 don’t show on the page, but the category 7 post does for some strange reason.

    Can anyone see a problem with this that makes it not work?

    esmi
    Forum Moderator

    @esmi

    Why not just use <?php query_posts( 'orderby=rand&posts_per_page=1&cat=4' ); ?>?

    Because that doesn’t work either. It still shows that one post from child category 7.

    esmi
    Forum Moderator

    @esmi

    Is that post also filed under cat 4?

    Nope.

    I think I’ll try recreating the category and post again. It doesn’t make any sense that it doesn’t work.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘excluding posts in a category with query_posts’ is closed to new replies.