# How do I taxonomy terms based on terms they are used alongside?

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

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?

• 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 Oct 20 '14 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 Oct 20 '14 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 Oct 23 '14 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 Oct 24 '14 at 8:35 ## 1 Answer 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',
array(
'taxonomy' => 'taxonomy_B',
'field'    => 'slug',
'terms'    => 'term_A',
),
array(
'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 Oct 28 '14 at 18:56
• Thanks @Pete, if you have any problems just comment and I will edit the answer to match your request. – Bordoni Oct 28 '14 at 19:28