最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Get category if used in a custom post type

programmeradmin10浏览0评论

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,
 ]
);
发布评论

评论列表(0)

  1. 暂无评论