List Posts For Terms Of A Custom Taxonomy For Any Post Type

The function is great and i am using this function with these filter to just display selected taxonomy term posts by id, but what result i am getting is like this

Child Taxonomy 1
– Child Taxonomy 1 Post 1
– Child Taxonomy 1 Post 2
– Child Taxonomy 1 Post 3

Read More

Child Taxonomy 2
– Child Taxonomy 2 Post 1
– Child Taxonomy 2 Post 2
– Child Taxonomy 2 Post 3

Parent Taxonomy
– Child Taxonomy 1 Post 1
– Child Taxonomy 1 Post 2
– Child Taxonomy 1 Post 3
– Child Taxonomy 2 Post 1
– Child Taxonomy 2 Post 2
– Child Taxonomy 2 Post 3

Means at the last after listing the Child Taxonomy Term Name and Its posts the function is also displaying the parent Taxonomy Name and All the posts from its Child taxonomies in parent Taxonomy as its posts. I need help to get rid of that parent taxonomy at the last and all posts from child taxonomies in it too, so it will just display the result like this:
“List All Child Taxonomies and Its Posts Only like below”

Child Taxonomy 1
– Child Taxonomy Post 1
– Child Taxonomy Post 2
– Child Taxonomy Post 3

Child Taxonomy 2
– Child Taxonomy 2 Post 1
– Child Taxonomy 2 Post 2
– Child Taxonomy 2 Post 3

What should i do in this function to get the result working code below:

Post Type = events
Post Type Custom Taxonomy Term = events_cat
Parent Taxonomy Term = announcments
i need this function to only list child taxonomies of announcments and their posts not the announcments too at the end with all posts from its child taxonomies.

Please help me how.

    // List posts by the terms for a custom taxonomy of any post type
$post_type = 'events';
$tax = 'events_cat';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
    foreach ($tax_terms  as $tax_term) {
    $args = array(
        'post_type' => $post_type,
        "$tax" => $tax_term->slug,
        'post_status' => 'publish',
        'posts_per_page' => 4,
        'caller_get_posts'=> 1
    );


$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $tax_term->slug,
),
array(
'taxonomy' => 'events_cat',
'field' => 'id',
'terms' => array( 3, 37 ),
'operator' => 'NOT IN',
)
),
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
        $my_query = null;
        $my_query = new WP_Query($args);

        if( $my_query->have_posts() ) : ?>

            <h2 class="breadcrumb">All <?php echo $tax; ?> Posts For <?php echo             $tax_term->name; ?></h2>
            <ul class="taxlist">
            <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

                <li id="post-<?php the_ID(); ?>">
                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>

            <?php endwhile; // end of loop ?>

            </ul>

        <?php else : ?>
        <?php endif; // if have_posts()
        wp_reset_query();

    } // end foreach #tax_terms
}

Related posts

Leave a Reply