1

I wasn't sure how to phrase my question, so I hope you understand.

I have two custom taxonomies with different terms in them.

Lets call them custom_tax1 & custom_tax2.

custom_tax1
term1
term2
term3
...

custom_tax2
term1
term2
term3
...

Question: Is it possible to make a list of all the terms from custom_tax2, that have posts tagged with term1 from custom_tax1?

0

1 Answer 1

1

I'm not sure if it's possible to directly query the terms, So I'm gonna write a workaround for you.

The WP_Query(); accepts an argument named terms. You can pass term IDs as an array to this argument.

Let's first fetch a list of terms in the first taxonomy, then query based on these terms:

$terms = get_terms( 
    array(
       'taxonomy'   => 'custom_tax1',
       'hide_empty' => false,
       'fields'     => 'ids', // We only need the IDs
   )
);
// Now do a query
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'custom_tax2',
            'field'    => 'slug',
            'terms'    => $terms,
        ),
    ),
);
$my_query = new WP_Query( $args );

This will query every post from custom_tax2 that has any of the terms from custom_tax1. Now that you have the posts that have these terms, you can proceed with extracting terms from them.

Let's crack this down a bit further:

if($my_query->have_posts()){
    // Define an empty array to use later
    $post_terms = array();
    while($my_query->have_posts()){
        $my_query->the_post();
        // Merge the previous terms with the new ones
        $new_terms = wp_get_post_terms( get_the_ID(), 'custom_tax1');
        $post_terms = array_merge( $post_terms, $new_terms );
    }
}

Here is what happened.

  1. We run a loop through all the posts belonging to custom_tax2 taxonomy that have the terms from custom_tax2 taxonomy.
  2. We get the terms of every post in the loop
  3. On each step of the loop we merge the current terms with terms from the previous post, so we end up with an array of terms from each post
5
  • Thanks, looks good, well thought! Trying to implement right now. I know how to return posts from the wp_query, but how do I output the slug for all the terms in custom_tax2 for example?
    – 2by
    Commented Aug 11, 2017 at 1:32
  • You're welcome. I've updated the answer for you.
    – Johansson
    Commented Aug 11, 2017 at 1:46
  • Thanks again! For some reason nothing is returned. I am trying to figure out why
    – 2by
    Commented Aug 11, 2017 at 2:01
  • Shouldn't it be possible to do a foreach and print the slugs?
    – 2by
    Commented Aug 11, 2017 at 2:14
  • You could try and var_dump variables to see what's going wrong. Since I don't have these structure, I'm not able to test the code on my machine. By the way, which slugs do you want to do a foreach to?
    – Johansson
    Commented Aug 11, 2017 at 2:48

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.