Support » Themes and Templates » Exclude terms from a taxonomy index of posts

  • Resolved Rose

    (@eos-rose)


    I found this useful bit of code somewhere:

    function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(), $wp_query_args = array() ){
        $tax_terms = get_terms( $taxonomy, $get_terms_args );
    
        if( $tax_terms ){
            foreach( $tax_terms  as $tax_term ){
                $query_args = array(
                    'post_type' => $post_type,
                    "$taxonomy" => $tax_term->slug,
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
    				'orderby'=> 'title',
    				'order' => 'ASC',
    				'exclude' => array( 2, 179 ),
                    'ignore_sticky_posts' => true
                );
                $query_args = wp_parse_args( $wp_query_args, $query_args );
    
                $my_query = new WP_Query( $query_args );
                if( $my_query->have_posts() ) {
    			?>
    
                    <h2 id="<?php echo $tax_term->slug; ?>" class="tax_term-heading"><?php echo $tax_term->name; ?></h2>
                    <ul>
                    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
                    <?php endwhile; ?>
                    </ul>
                    <?php
                }
                wp_reset_query();
            }
        }
    }

    It works a treat to list all terms in a taxonomy and all posts under each term, but there are a few terms that I don’t care to include. I inserted 'exclude' => array( 2, 179 ), into the $query_args array as “2” and “179” are the term ids in question, but this seems to have had no effect whatsoever.

    Can someone tell me what I need to do to exclude these two terms from my results?

  • The topic ‘Exclude terms from a taxonomy index of posts’ is closed to new replies.