4

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

6

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.

1
  • Short and sweet. Much appreciated!
    – deathlock
    Jul 2, 2017 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.