custom post types - get taxonomies from terms

admin2025-03-19  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']);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742372246a210215.html

最新回复(0)