I want to exclude multiple terms in get_terms, but the exclude parameter only accepts term IDs. So I have to get the term IDs from its slug.

This is what I have so far:

$pres = get_term_by('slug', 'president', $cat_type);
$vice = get_term_by('slug', 'vice-president', $cat_type);
$admin = get_term_by('slug', 'admin', $cat_type);
$rnd = get_term_by('slug', 'rnd', $cat_type);

$ID_pres = $pres->term_id;
$ID_vice = $vice->term_id;
$ID_admin = $admin->term_id;
$ID_rnd = $rnd->term_id;

$terminologies = get_terms( $cat_type, array(
                    'orderby'   => 'term_id',
                    'exclude'   => '???' <<<<
                ) );

I felt there’s something really wrong and redundant here but I’m not sure how to put them into an array that can be used in theexclude parameter. Any help?

1 Answer
1

You can get terms, from multiple slugs, with the slug argument:

$exclude = get_terms ( 
    [ 
        'slug'     => [ 'president', 'vice-president', 'admin', 'rnd' ], 
        'taxonomy' => $cat_type,
        'fields'   => 'ids',
    ] 
);

where we use the fields argument to return only term ids.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *