Exclude specific slug in ‘get_terms’

The get_terms() (see docs) function accepts the same args as WP_Term_Query(see docs)
You have to get those terms Ids first and then pass it to the exclude arg:

// default to not exclude terms
$ids_to_exclude = array();
$get_terms_to_exclude =  get_terms(
    array(
        'fields'  => 'ids',
        'slug'    => array( 
            'graduate', 
            'job-market-candidate', 
            'graduate-student',
            'research' ),
        'taxonomy' => 'role',
    )
);
if( !is_wp_error( $get_terms_to_exclude ) && count($get_terms_to_exclude) > 0){
    $ids_to_exclude = $get_terms_to_exclude; 
}
$roles = get_terms(
    array(
        'orderby'    => 'ID',
        'order'      => 'ASC',
        'hide_empty' => true,
        'exclude'    => $ids_to_exclude,
        'taxonomy'   => 'role',
    )
);

$count_roles = count($roles);

if ( $count_roles > 0 ) : ?>
     //do stuff
<?php endif;?>

Leave a Comment