Get unique post in parent category


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.

, advcha 4 years 2020-05-19T10:11:20-05:00 0 Answers 94 views 0

Leave an answer