How do I get all of a single taxonomies terms that have one or more particular sibling terms?

For example, let's say I have the following taxonomies. Terms are listed underneath the taxonomies, and the parenthesis indicates have many posts the terms are used on.

age taxonomy

  • sixteen (used on 3 posts)
  • twenty-seven (used on 0 posts)
  • thirty-four (used on 12 posts)

eye_color taxonomy

  • Blue (used on 5 posts)
  • Brown (used on 10 posts)
  • Green (used on 0 posts)

My desire is to get a list of all of the terms within eye_color that:

  1. have posts
  2. are used alongside thirty-four on one or more posts

Of course, I know that get_terms() has a hide_empty argument, but that only accomplishes half of what I want.

UPDATE: A comment below links to a function that allows you to find terms based on sibling terms of a single taxonomy...but I'd like to find terms based on sibling terms of multiple taxonomies.

In plain english, I'd basically like to:

Select all terms from taxonomy A that are used on a post with both Term 1 from Taxonomy B and Term 2 from Taxonomy C.

Any ideas?

  • 1
    I have a Gist for that: gist.github.com/Giuseppe-Mazzapica/11190603 nothing to be proud of (it works, but it's a monster...)
    – gmazzap
    Commented Oct 20, 2014 at 18:38
  • Link-only answers are not a good way to go, and paste that bunch of code here without explaination is a thing I would avoid... Thanks, anyway.
    – gmazzap
    Commented Oct 20, 2014 at 19:05
  • Do you have any suggestions as to how I might modify this to allow me to specify multiple $terms of DIFFERENT taxonomies?
    – Pete
    Commented Oct 23, 2014 at 14:41
  • Probably you should convert first 2 funcion arguments to 1 argument: an array of taxonomies (as keyx) and terms (as values). Then modify function logic accordingly. 3rd argument of the function is already an array so it accepts multiple taxonomies.
    – gmazzap
    Commented Oct 24, 2014 at 8:35

1 Answer 1


So my solution on this kind of problems where we have a very complex set of rules is to avoid doing new SQL just for my solution because if you have to explain it to a beginner it gets hairy.

My solution:

function get_term_union( $taxonomy, $tax_query, $post_type = 'post' ){
    $args = array(
        'post_type' => $post_type,
        'tax_query' => $tax_query,
        'fields' => 'ids',
    $posts = new WP_Query( $args );

    $terms = array();

    foreach( $posts as $post_id ){
        $terms = array_merge( $terms, wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) ) );

    $terms = array_unique( $terms );

    return $terms;

// Usage
$tax_query = array(
    'relation' => 'AND',
        'taxonomy' => 'taxonomy_B',
        'field'    => 'slug',
        'terms'    => 'term_A',
        'taxonomy' => 'taxonomy_C',
        'field'    => 'slug',
        'terms'    => 'term_B',

// This will return you all the term id's for `taxonomy_A`
var_dump( get_term_union( 'taxonomy_A', $tax_query ) );

If this solution doesn't fit your needs tell me and I will try to fix it.

My Best Regards,

  • Great! Thanks! I haven't had a chance to test yet, but it looks like exactly what I need. Because the bounty time limit was running out I've gone ahead and awarded you the bounty. Thanks!
    – Pete
    Commented Oct 28, 2014 at 18:56
  • Thanks @Pete, if you have any problems just comment and I will edit the answer to match your request.
    – Bordoni
    Commented Oct 28, 2014 at 19:28

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.