Support » Fixing WordPress » Exclude posts from children categories with query_posts/get_posts

  • Hi, I’d like to exclude posts from children categories with query_posts() or get_posts(), i.e. so that only posts directly related to the active category are shown.
    Is there any way to do this?


Viewing 3 replies - 1 through 3 (of 3 total)
  • In theory, if your ‘active category’ is a category parent, and you want to exclude posts from the children, then you would get NO posts displayed, because the parent category should not be assigned to a post–only the lowest/deepest child in the hierarchy should be checked.

    If both the parent and child categories are assigned to posts, then this should work to return ‘Category ID 6 only’ posts:

    query_posts(array('category__in' => array(6)));

    Post from my children/grandchildren categories are showing up in the parent categories and ordered by date. So they will show up before
    the current categories post if published later. I don not have each category checked in the post, just the appropriate one.

    I just want the current categories post to take precedence. So
    House cat has a post, its children/grandchildren have posts. If on the parent ‘House’ category that post should show first.

    I am now using the code listed above but like so:

    $cat_posts->query(array('category__in' => array($cat),'posts_per_page'=>1,'order'=>DESC));

    I now cant have previous links for the subcategory articles though. Is this the completely wrong way to handle a category-based theme.

    Thank you for any insight.

    Thanks, MDR21, and MichaelH.
    It works for me. I can now list all of a parent category’s posts, then display a list of subcategories underneath.

    Still working on getting the list of subcats nice and purdy.

    <div class="content_container">
    		<h1 class="first">Produkte in aktueller Kategorie:</h1>
    		<?php query_posts(array('category__in' => array($cat))); ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<div class="zwei_drittel"><div class="image_medium"><a href="<?php the_permalink(); ?>"><div class="image_medium_border"></div><?php postimage_special(300,140); ?></a></div></div>
    		<div class="ein_drittel"><h2 class="notoppadding"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><?php the_excerpt(); ?></div><div class="clear"></div>
    		<?php endwhile; else: ?><p>index.php | nichts gefunden</p><?php endif; ?>
    		$this_category = get_category($cat);
    		// If this category contains children
    		if (get_category_children($this_category->cat_ID) != "") {
    			echo '<h1 class="first">Unterkategorien</h1>';
    			echo '<ul>';
    			echo '</ul>';
    		} ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude posts from children categories with query_posts/get_posts’ is closed to new replies.