WordPress.org

Ready to get started?Download WordPress

Forums

excluding posts in a category with query_posts (6 posts)

  1. yolise
    Member
    Posted 3 years ago #

    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]

  2. yolise
    Member
    Posted 3 years ago #

    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?

  3. esmi
    Forum Moderator
    Posted 3 years ago #

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

  4. yolise
    Member
    Posted 3 years ago #

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

  5. esmi
    Forum Moderator
    Posted 3 years ago #

    Is that post also filed under cat 4?

  6. yolise
    Member
    Posted 3 years ago #

    Nope.

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

Topic Closed

This topic has been closed to new replies.

About this Topic