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

custom post types - get taxonomies from terms

programmeradmin0浏览0评论

I have two post types

  1. Documents
  2. News Items

with categories for each post type,

  1. Documents -- PDF, Word, Excel
  2. News Items -- Articles, Releases

BUT they share the same tags.

  1. Documents -- BOSS, EFF, CDF
  2. News Items -- BOSS, EFF, CDF

How can I retrieve a list of the categories whose posts( or post count. regardless of post type) have a specified tags e.g.

BOSS -- PDF(2), Releases (1)

CDF -- Articles (1), Excel (1)

I have two post types

  1. Documents
  2. News Items

with categories for each post type,

  1. Documents -- PDF, Word, Excel
  2. News Items -- Articles, Releases

BUT they share the same tags.

  1. Documents -- BOSS, EFF, CDF
  2. News Items -- BOSS, EFF, CDF

How can I retrieve a list of the categories whose posts( or post count. regardless of post type) have a specified tags e.g.

BOSS -- PDF(2), Releases (1)

CDF -- Articles (1), Excel (1)

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Jun 5, 2020 at 15:17 KendallKendall 1891 gold badge1 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After some research I came up with the following

// Get the posts in that category with the required tag
$args = array(
    'post_type' => ['documents','news'],
    'fields'          => 'ids', // Only get post IDs
    'posts_per_page'  => -1,
    'tax_query'      => array(
        array(
            'taxonomy'  => 'forms',
            'field'     => 'slug',
            'terms'     => $symbol
        )
    )
);
$posts_array = get_posts( $args );
$categories = wp_get_object_terms($posts_array, ['sources','articles'], ['fields' => 'id=>name']);
发布评论

评论列表(0)

  1. 暂无评论