Put the following in your functions.php
:
function wp_list_categories_for_post_type($post_type, $args="") {
$exclude = array();
// Check ALL categories for posts of given post type
foreach (get_categories() as $category) {
$posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));
// If no posts found, ...
if (empty($posts))
// ...add category to exclude list
$exclude[] = $category->cat_ID;
}
// Set up args
if (! empty($exclude)) {
$args .= ('' === $args) ? '' : '&';
$args .= 'exclude=".implode(",', $exclude);
}
// List categories
wp_list_categories($args);
}
Now you can call wp_list_categories_for_post_type('photos');
or wp_list_categories_for_post_type('videos', 'order=DESC&title_li=Cats');
and the like.
Related Posts:
- How can I list all the categories under a Custom Post Type (taxonomy)?
- Custom Posts Types using Posts Category
- How to get a list of term names of the custom post type im currently on in single.php
- Regex problem in an add_rewrite_rule
- Categories or Custom Post Type to Organize Content
- On clicking on the category name the page is redirecting on 404 pge
- Hook onto Add New Category
- Custom edit post column – category not showing
- Custom post type taxonomy template
- Display Posts of a Category in Alphabetical Order (Custom Post Type)
- Taxonomy structure of Sport site
- get_categories for custom post type and filter by custom taxonomy (brand) and list child categories of a defined category
- Page naming when there is a sub-category only sometimes.
- get custom post type categories
- Custom post types and permalink
- Category page only displaying the posts from a custom type
- Get a permalink structure of /%posttype%/%category%/%postname%
- Search doesn’t find tags or categories in custom post types
- Get Post Primary Category
- Pagination on category page with custom post types
- Call custom post type by category
- Add category attribute to custom shortcode
- Should I use custom menu, C.P.T. or theme options, or something else for this?
- Posts from all the categories are being displayed instead of particular category
- Show custom post type filtered by category
- Categories sorting
- How can I make my custom posts appear in their assigned category url?
- Allow user to set custom order to a list of custom taxonomies?
- forming WP_Query for posts of all post types but from specific categories
- PHP variable not regenerating when publishing multiple posts at the same time
- Filter CPT posts by one or more categories
- Custom Post Types with a common category for a blog listing
- WP Query with categories only shows one post and ignores the category
- Custom Post Type + Categories
- Hierarchy and access control for Custom Post Types (CPT)
- Loop filtering Custom Post Types and/or Categories
- Best method to make posts searchable, sortable and filterable – custom field, tag or category?
- Conditional statement for if archive page has posts which contain certain taxonomies/categories/tags, show those terms
- How can I show 1 featured post in a styled element, and the next few below differently styled
- How do you create a custom template to display a category with an image and related posts below?
- How can I list Custom Post Types created with the Types plugin under categories?
- Category.php template for custom posts
- Categories and tags for custom post types
- Archieve.php not loading for custom post type
- Category archive in with conjunction with custom post type is empty
- How to add current custom taxonomy slug to body class
- Meta data (Tags and Categories) for Custom Posts not showing.
- List Posts By Custom Taxonomy
- How to Display Posts From Category Within a Custom Taxonomy?
- Custom post types with categories in template
- Need help deciding on a taxonomy
- Category Icon on custom post type
- The Difference Between Categories and Tags and Taxonomies and Terms
- Get assigned post categories
- Default Category Page not showing custom post type which has taxonomy category
- Custom Post Type posts not showing in category
- Post Query not working
- Get child categories of custom taxonomy category?
- Create query for both custom post type and category
- Get terms for a specfic post from multiple taxonomies in custom post type
- Category Name with Custom Post Type
- cannot achieve this structure (custom posts VS categories VS ??)
- Am I mixing up the concept of posts pages and categories?
- Custom post type, taxonomy and admin bar
- CPT archive page – show one post from each taxonomy term
- how to organize my categories or should I do custom post types for some?
- Order Categories by Hierarchy?
- Multiple categories assigned to a single product breaking the breadcrumb
- How to divide Subcategories into pages of parent category wordpress
- Display all Categories except ones with a specific parent
- Custom front-end form for adding post – Category problem
- Custom Post Type Category Displaying ALL Sub Categories as well
- Control over custom post types on a specific page
- get taxonomies from terms
- create a “add icon” field in taxonomies page
- Feature image Gallery made from Custom Post type + Categories
- Show Custom Post Type based on Category on Archive Page
- To display the category of a specific custom post type
- Get meta values from parent post and save in child post
- Use Category for Custom Post Type – But Need Another Separate Archive Page too
- Only show current category post
- Custom post type category archive URL redirects to home page
- how to use two permalinks for one custom post type based on categories
- Structuring 3K pages in WordPress
- Organizing The Custom Post Type with Taxonomies / Parent Posts
- Display 2nd category, only once, as sub-heading, in the loop
- Custom Catagory not found
- Page to show custom posts and one category
- wp_list_categories() with attachment post types
- How can I list custom post by custom category?
- category_name not working in WP_Query
- How can I set up the URL for a category archive for a custom post type?
- how to show perticular category posts in custome page
- Use only selected regular categories for a Custom post form
- Bulk remove category from custom post type?
- Page that ‘subscribes’ to multiple categories
- Problem with menu categories doubling up when updating database?
- Remove slug from Custom Category Permalink + dual-category permalinks
- How to query posts by category with the_title();
- Can WordPress show posts based on a button that the user clicked 2 pages back?