I’m afraid there won’t be a nice way to do this…
One way will be to:
- get all the found posts,
- for every one of them get its terms,
- count the terms.
But it won’t be very efficient and I wouldn’t recommend this way of doing it.
On the other hand there is an efficient, but not very nice way of doing it – with SQL query:
$query = new WP_Query(array('post_type' => 'foo', 'numberposts' => 20));
$ids = wp_list_pluck($query->posts, 'ID');
$placeholders = array_fill(0, count($ids), '%d');
$format = implode(', ', $placeholders);
$results = $wpdb->get_results( $wpdb->prepare(
" SELECT terms.name, COUNT(tr.object_id) as count FROM {$wpdb->terms} terms " .
" INNER JOIN {$wpdb->term_taxonomy} tt ON (tt.term_id = terms.term_id) " .
" INNER JOIN {$wpdb->term_relationships} tr ON (tr.term_taxonomy_id = tt.term_taxonomy_id) " .
" WHERE tr.object_id IN ({$format}) GROUP BY terms.term_id ",
$ids
) );
Related Posts:
- Get terms by taxonomy AND post_type
- Display current taxonomy term when inside custom post type
- Get term slug of current post
- How do I list custom taxonomy terms without the links?
- query multiple taxonomies
- wp_set_object_terms() is not replacing term, but creating a new one
- Get terms that contain posts that in turn belong to other terms?
- wp_insert_term doesn’t work with custom post type’s taxonomy
- Custom Taxonomies Terms as Post Title for Custom Post Types upon Publishing
- Retrieve single term slug
- How can I add programmatically custom taxonomy terms to a custom type post when saving posts?
- Glossary with Custom Post Type
- Custom Post Type Archive Template
- Maintaining strict one-to-one association between terms and custom posts
- Retrieve a specific field from taxonomy term through advanced custom fields [closed]
- If on term-page -> get the current term?
- Sort the main query in subcategories/terms?
- Get list of terms of current taxonomy archive for another taxonomy
- Get all taxonomies for all post types
- Dynamically create/remove terms in taxonomy when custom post type is published/trashed
- How to get the first term for the current taxonomy?
- How do I provide a “show all posts” link in a paginated term archive?
- Output the content of a term organised by each of the CPT?
- How to filter the taxonomy terms based on another taxonomy term
- How to output wordpress custom tags separated by comma?
- get_the_terms return only last term
- Does WordPress limit the length of slug names for Post Meta or Terms?
- get_post_meta returns empty array for terms
- How do I display tags for a custom post type single page?
- Custom Post Type Archive Page Filtering
- Get posts by querying taxonomy and certain terms of the taxonomy?
- Show posts from two or more custom taxonomy terms
- Display post as term id
- Issue on Creating Custom Tax Term Dynamically From Another CPT Meta-box On Publish
- Post count by month of taxonmy term
- WordPress loop: Show only a Custom Post Type Taxononmy TERM
- Display Custom Taxonomy Alphabetically
- Get terms that contain posts that in turn belong to other terms?
- How to get custom posts sub category link
- Custom while loop for hierarchical display of a taxonomy
- Displaying custom-taxonomy-terms sorted by parent/child-hierarchy?
- Listing all term items alphabetically / sorting loop
- How to get WordPress term attached to the Post?
- Get parent category id from child category page for custom taxonomy
- How to get only child terms from a custom taxonomy of current post type?
- How to Display Posts From Category Within a Custom Taxonomy?
- Custom Post Types Archives and Single Pages not showing custom taxonomy data
- Get terms of a post but only if they’re also the child of a specific term
- The Difference Between Categories and Tags and Taxonomies and Terms
- How to display custom taxonomy term meta on custom post type
- how to count the current posts terms
- Get the link of the first post of a custom taxonomy in a custom taxonomy list
- Commas not displaying in implode
- does wp_insert_term link the term to a certain post ID?
- How can I get 3 different taxonomy type terms in a div class element?
- Grab all Custom Posts by multiple taxonomies and terms
- Get terms for a specfic post from multiple taxonomies in custom post type
- Add custom post type taxonomy tag to article class
- Custom Taxonomy dont save in a frontend form for post a custom post
- Using get_terms() to list terms from one custom taxonomy AND from one specific built-in category
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- display post count in archive page that have relation with another taxonomy term
- How to display custom taxonomy term specific post?
- Excluded Custom Taxonomy Term Posts Displaying in loop
- How to associate custom taxonomy terms with custom post type?
- get_queried_object not work in taxonomy page
- Change the term based on the value of a $variable using wp_update_post in submitting a form
- get_the_terms child terms for current post/custom post only
- Get all posts for custom taxonomy term
- posts_per_page is not working by term
- Dropwdown: Get taxonomy terms from posts that share preselected terms from 3 other taxonomies
- Custom Post Type / Taxonomy / Term – what template to use to avoid 404-page?
- Pages of my taxonomy terms are showing all posts
- Display title of all custom posts from same taxonomy term on a custom post template
- wordpress does not see the correct custom taxonomy hierarchy
- Add active class to foundation 6 tabs while looping categories
- Saving Child Terms on front end not setting parent
- Get category list & taxonomy list from different post type using post id?
- How to get all posts related to a taxonomy?
- Custom taxonomy- only output relevant terms
- Filter By Term Not Working – Custom Post Type
- Get posts of an specific term of a custom taxonomy
- WordPress add taxonomies/terms list as a menu in archive page
- Hide Custom Taxonomy title if not associated with another Custom Taxomony
- How to pass taxonomy terms to WP_Query along with $args?
- Custom Post Status & Taxonomies
- Fetch posts list from fist CPT taxonomy term and list under 2. CPT
- Prevent repetitive terms in get_the_term_list
- How to List CPTs Under One Tax Term
- Get a list of categories ids
- Taxonomy Query of Custom Post Displays Archive Instead of Posts
- List custom taxonomy terms
- Display associated taxonomy child name on single CPT page
- How to manage a bookstore
- Get all terms assigned to a post from different taxonomies
- Issue On Listing Woocommerce Parent Tag List
- How to use wp_set_object_terms depending on page ID?
- Taxonomy Archive URL + Template
- Is it possible to get_terms by taxonomy AND post_type?
- Is there a way to edit non-custom term attributes?