1

I have a CPT with the following taxonomies: category, taxonomy_1 and taxonomy_2 (the last two are non-hierarchical).

I want to get programmatically all the terms of the "category" taxonomy but only of the posts with a given meta value — that meta value is storing the ID of another taxonomy (taxonomy_2).

So it would be: Categories -> Posts -> Specific meta value

I've tried the following without success:

$args = array(
    'meta_query' => array(
        'key'       => 'taxonomy_key_in_CPT',
        'value'     => 'value_of_taxonomy_key_in_CPT',
        'compare'   => "=",
    )
)

$categories = get_categories( $args );

But this will still return all the categories under the CPT. Am I doing something funny?

1 Answer 1

0

Welcome to WPSE. I believe you are looking for the get_terms() function. This will return all of the terms for a particular taxonomy, and not the posts.

Pass your taxonomy slug in the array of arguments and set the hide_empty to false so you get a complete list, like this:

$terms = get_terms( array(
    'taxonomy' => 'taxonomy_1',
    'hide_empty' => false,
) );

Full documentation can be found code reference: https://developer.wordpress.org/reference/functions/get_terms/

2
  • Thank you @jdm2112. I'm actually trying to get the category taxonomy only used in posts using another taxonomy. Am I making sense hehe? I think I need to update my question. Commented Dec 5, 2019 at 16:39
  • I updated my question. And I think that were I got confused is that the meta_query is looking for meta values for the category taxonomy right? Not on the posts... lol Commented Dec 5, 2019 at 16:45

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.