Support » Plugins » Hacks » Excluding top level from taxonomy and post loop

  • I’ve got a custom post type / taxonomy. I’ve created a list that shows all the categories and the posts in them.

    I have set up a top level category (taxonomy) that has two sub categories. I want my list to show only the second level categories and the posts in them. Currently it outputs both the sub categories and their posts correctly but it then also shows the top level category and all the posts within that too even if they are not directly in it.

    Sub Category 1
    •Post 1
    •Post 2

    Sub Category 2
    •Post 3
    •Post 4

    Top Level Category
    •Post 1
    •Post 2
    •Post 3
    •Post 4

    How do I exclude the top level category and it’s list completly? E.g.

    Sub Category 1
    •Post 1
    •Post 2

    Sub Category 2
    •Post 3
    •Post 4

    Current code

    <?php
        $custom_terms = get_terms('service_categories');
    
        foreach($custom_terms as $custom_term) {
            wp_reset_query();
            $args = array('post_type' => 'services',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'service_categories',
                        'field' => 'slug',
                        'terms' => $custom_term->slug,
                    ),
                ),
             );
    
             $loop = new WP_Query($args);
             if($loop->have_posts()) {
                echo '<h2>'.$custom_term->name.'</h2>';
    
                while($loop->have_posts()) : $loop->the_post();
                 $title = get_the_title();
                 $url = get_permalink();
                 $serviceicon = get_field( 'select_icon' );
                 ?>
    
                    <div class="left-list2-box">
                        <a href="<?php echo $url; ?>">
                            <div class="left-list2-icon">
                                <div class="list-services-icons <?php echo $serviceicon ?>"></div>
                            </div>
                            <div class="left-list2-title">
                                <?php echo $title; ?>
                            </div>
                        </a>
                    </div>
    
                <?php endwhile;
             }
        }
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can specify a second parameter in get_terms to exclude specific term IDs:

    $term_args = array(
    	'exclude' => array(5, 8)
    );
    
    $custom_terms = get_terms('service_categories', $term_args);

    In the case above, we’re excluding terms with ID 5 and 8.

    If you need something more dynamic and want to hide any term that does not have a parent (which would make it a parent), you could add something like this before your foreach loop:

    if ($custom_term->parent === 0) {
    	continue;
    }

    The first one worked perfectly.

    So simple.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Excluding top level from taxonomy and post loop’ is closed to new replies.