Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a custom page-template to pull a custom post types (education) custom taxonomy (animals).

What I'm trying to do is display all child posts + term (set as h2) inside the term "livestock", without displaying the parent.

The query below shows all child posts under the appropriate term but also the parent (+ all posts signed to its children). Is there a way to modify this query to ignore the parent and just display the child terms + the posts within each of those terms?

ex:

Bison

  • Post 1

  • Post 2

  • Post 3
  • Post 4

Cows

  • Post 1
  • Post 2

Horses

  • Post 1
  • Post 2
  • Post 3

Livestock (don't want this term + posts to show up)

  • Post 1
  • Post 2
  • Post 3
  • Post 4

    <?php $custom_terms = get_terms('animals');
            //find a way to remove parent (ie show all except for pet-care) 
            foreach($custom_terms as $custom_term) {
                wp_reset_query();
                $args = array('post_type' => 'education',
                    'animals' => 'livestock',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'animals',
                            'field' => 'slug',
                            'terms' => $custom_term->slug,
                        ),
                    ),
                 );
    
                 $loop = new WP_Query($args);
                 if($loop->have_posts()) {
                    echo '<h2>'.$custom_term->name.'</h2>';
                    echo '<dl class="vertical tabs">';
                    while($loop->have_posts()) : $loop->the_post();
                        echo '<dd><a href="'.get_permalink().'" title="' . get_the_title() .'">'.get_the_title().'</a></dd>';
                    endwhile;
                    echo '</dl>';
                 } 
            } ?>
    

Edit: solved

Looks like I overlooked 'operator' => 'AND' parameter on tax_query.

The following appears to be excluding the parent terms but showing all child terms along with the posts associated with each custom taxonomy's term.

What a sigh of relief that is!

    <?php $custom_terms = get_terms('animals');
            //find a way to remove parent (ie show all except for pet-care) 
            foreach($custom_terms as $custom_term) {
                wp_reset_query();
                $args = array('post_type' => 'education',
                    'animals' => 'livestock',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'animals',
                            'field' => 'slug',
                            'terms' => $custom_term->slug,
                            'operator' => 'AND',
                        ),
                    ),
                 );

                 $loop = new WP_Query($args);
                 if($loop->have_posts()) {
                    echo '<h2>'.$custom_term->name.'</h2>';
                    echo '<dl class="vertical tabs">';
                    while($loop->have_posts()) : $loop->the_post();
                        echo '<dd><a href="'.get_permalink().'" title="' . get_the_title() .'">'.get_the_title().'</a></dd>';
                    endwhile;
                    echo '</dl>';
                 } 
            } ?>
share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.