I'm using WordPress's get_terms to return an array of WP_Term objects containing the term_id, name, slug, etc. for the child terms of a given taxonomy. I've saved this array to $tax_terms. I have a different array—$active_filters—containing just the slugs of terms that match certain taxonomy terms; for instance, from a var_dump of $active_filters:

array (size=3)
  0 => string 'term-1' (length=6)
  1 => string 'term-2' (length=6)
  2 => string 'term-3' (length=6)

I'm wondering if there's a way I can run a foreach that will compare this array of term slugs to those in the array returned by get_terms and return the name for any matched slug? So, the example above would output "Term 1", "Term 2", "Term 3". I've tried using array_search but to no avail, thus far. Any assistance is greatly appreciated.

1 Answer 1


Perhaps array_map ?

This will give you an array of term names which matched your slugs in $active_filters:

$matched_terms = array_map(function($term) use ($active_filters){
    if(in_array($term->slug, $active_filters)){
        return $term->name;
        return false;

}, $tax_terms);

//remove the empty array elements which came from terms which didn't match a slug
$matched_terms = array_filter($matched_terms);

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.