categories - Get category if used in a custom post type

I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categ

I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categories taxonomy. There are also other custom post types that use the same default WP category.

Is it possible to add a meta_query that checks if the category is used in a Custom post_type? eq: custom post type: work.

$work_categorys = get_terms(
 [ 
   'taxonomy' => "category", 
   'hide_empty' => true,
 ]
);

foreach ($work_categorys as $key => $value) { echo '<li data-filter-tag="'.$value->slug.'" class="">'.$value->name.'</li>'; }

I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categories taxonomy. There are also other custom post types that use the same default WP category.

Is it possible to add a meta_query that checks if the category is used in a Custom post_type? eq: custom post type: work.

$work_categorys = get_terms(
 [ 
   'taxonomy' => "category", 
   'hide_empty' => true,
 ]
);

foreach ($work_categorys as $key => $value) { echo '<li data-filter-tag="'.$value->slug.'" class="">'.$value->name.'</li>'; }
Share Improve this question asked Jan 29, 2020 at 9:45 BonttimoBonttimo 1932 silver badges5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

This would work. As posted by @bucketpress.

$someposts = get_posts(
    array(
        'post_type' => 'work',
        'posts_per_page' => -1,
        'fields' => 'ids', // return an array of ids
    )
);

$somepoststerms = get_terms(
    array(
        'taxonomy' => 'category',
        'object_ids' => $someposts,
        'hide_empty' => true,
    )
);

To get all terms from all taxonomies assigned to "work" custom post type you can customize your code to look like this

$taxes = array_keys( get_object_taxonomies( 'work', 'objects' ) );
$work_categorys = get_terms(
 [ 
   'taxonomy' => $taxes, 
   'hide_empty' => true,
 ]
);

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

相关推荐

  • categories - Get category if used in a custom post type

    I'm trying to get a list of categories if they are used in a Custom Post type. The post type uses the default categ

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信