Getting categories of posts under a custom taxonomy

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

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?

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?

Share Improve this question edited Dec 5, 2019 at 16:44 darkflamemaster asked Dec 5, 2019 at 15:34 darkflamemasterdarkflamemaster 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/reference/functions/get_terms/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744936235a4602042.html

相关推荐

  • Getting categories of posts under a custom taxonomy

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

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信