WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a parent category and several child categories. I also have several posts that belong to the child categories. Each post can be belong to several categories. I want to know how to query the post table so when I want to show all posts in parent category, I'll get all the post under the child categories. the result have to unique post. Sorry for my bad english.

Here is my code :

$cats = get_categories('child_of=95'); 

// loop through the categries
foreach ( $cats as $cat ) {
    // setup the cateogory ID
    $cat_id = $cat->term_id;
    // Make a header for the cateogry
    //echo "<h2>".$cat->name."</h2>";
    // create a custom WordPress query
    // start the wordpress loop!
    if (have_posts()) : while (have_posts()) : the_post(); ?>

        <?php // create our link now that the post is setup ?>
        <h2><?php the_title(); ?></h2>
        <a href="<?php the_permalink();?>"><?php the_content(); ?></a>
        <?php echo '<hr/>'; ?>

    <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>

But I always get the duplicate posts.

share|improve this question
are you get only subcategory post's excluding parentcategory? – ravi patel Jan 13 '14 at 7:22

you can easily find solutions for this by using google.

-- edit with solution for duplicates -- to filter out your duplicates fill an array with the ids. then check if the post is in the array if not print it , otherwise skip it. you said you have solution for getting the posts so I won't be typing code for that

$do_not_duplicate = array(); // set before loop variable as array

while ( have_posts() ) : the_post();
$do_not_duplicate[] = $post->ID; // remember ID's in loop

//now loop through the array
if ( !in_array( $post->ID, $do_not_duplicate ) ) { // check IDs
// display posts ...

share|improve this answer
i can find the solution but not for unique post. – advcha Aug 7 '12 at 20:15
you should fill an array with the ids. so first add the id to your array, next, check if the id, is already in the array. Then don't print it. – woony Aug 7 '12 at 20:17
i try to find the posts by the parent category – advcha Aug 7 '12 at 20:20

The WordPress loop should be able to handle this for you easily (unless I'm not understanding your question).

Whether you're using the loop and query_posts or your own instance of WP_Query, simply pass the proper category arguments.


Parent Category (ID: 1)

Child Category 1 (ID: 2)

Child Category 2 (ID: 3)

$args = array (
  'cat' -> 1, // Category ID of Parent Category (in this case, 1)
  'posts_per_page' -> 10 // Show 10 posts from this category, set to -1 to show all
if ( have_posts() ) : while ( have_posts() ) : the_post(); 
  <!-- Do something -->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.