WordPress.org

Ready to get started?Download WordPress

Forums

Exclude posts from children categories with query_posts/get_posts (4 posts)

  1. prism9
    Member
    Posted 5 years ago #

    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?

    Thanks!

  2. MichaelH
    Member
    Posted 5 years ago #

    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)));
  3. mdr21
    Member
    Posted 5 years ago #

    Hello,
    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.

  4. mores
    Member
    Posted 4 years ago #

    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.
    http://prilisauer.eu/category/lebensmittelverpackung/

    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; ?>
    
    		<?php
    		$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>';
    	wp_list_categories('hide_empty=0&title_li=&orderby=term_order&child_of='.$this_category->cat_ID);
    			echo '</ul>';
    		} ?>
    
    	</div>

Topic Closed

This topic has been closed to new replies.

About this Topic