I have three categories: Money, Space, Advice
.
I use these on a post index page to filter through all of my posts by creating checkboxes for each category type.
@php $categories = get_categories() @endphp
@foreach($categories as $category)
<div class="custom-control custom-control-inline custom-checkbox">
<input type="checkbox" name="{{ $category->slug }}" value=".{{ $category->slug }}" class="custom-control-input" id="{{ $category->slug }}">
<label class="custom-control-label" for="{{ $category->slug }}">{{ $category->name }}</label>
</div>
@endforeach
This creates a checkbox list of every category in use.
Is there a way to limit get_the_categories()
to specific post types?
For instance, if I have a post type called team-member
I still want to use Money, Space, Advice
for categorisation but I don't want filters to appear if the categories are unused for that post type.
However, using get_the_categories()
would display used categories in any post type, this means that for a custom post type there may not be anything categorised but the filter would display, which is misleading.
So, I'm wondering if there's something like get_the_categories('team_member)
?
I have three categories: Money, Space, Advice
.
I use these on a post index page to filter through all of my posts by creating checkboxes for each category type.
@php $categories = get_categories() @endphp
@foreach($categories as $category)
<div class="custom-control custom-control-inline custom-checkbox">
<input type="checkbox" name="{{ $category->slug }}" value=".{{ $category->slug }}" class="custom-control-input" id="{{ $category->slug }}">
<label class="custom-control-label" for="{{ $category->slug }}">{{ $category->name }}</label>
</div>
@endforeach
This creates a checkbox list of every category in use.
Is there a way to limit get_the_categories()
to specific post types?
For instance, if I have a post type called team-member
I still want to use Money, Space, Advice
for categorisation but I don't want filters to appear if the categories are unused for that post type.
However, using get_the_categories()
would display used categories in any post type, this means that for a custom post type there may not be anything categorised but the filter would display, which is misleading.
So, I'm wondering if there's something like get_the_categories('team_member)
?
1 Answer
Reset to default 2The problem is that categories don't store any information about which post types it's been used on, just the total count. So if you want to know if a category is being used for a particular post type, you need to query all the posts in that category, and check which post type they are. There isn't a WordPress function that does this, so you'd need to make one. The most efficient way to do this would be a probably be a custom SQL query.
function wpse_330111_get_categories_for_post_type( $post_type ) {
global $wpdb;
/**
* The SQL query for getting categories based on which post type they've
* been used on.
*/
$query = $wpdb->prepare(
"SELECT DISTINCT
t.*, tt.*
FROM
$wpdb->terms t
INNER JOIN
$wpdb->term_taxonomy tt ON
tt.term_id = t.term_id
LEFT JOIN
$wpdb->term_relationships tr ON
tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN
$wpdb->posts p ON
p.ID = tr.object_id
WHERE
tt.taxonomy = 'category' AND
p.post_status = 'publish' AND
p.post_type = %s
",
$post_type
);
$categories = $wpdb->get_results( $query );
/**
* If there's results, make sure to return a proper WP_Term object for each
* category.
*/
if ( ! empty( $categories ) ) {
$categories = array_map( 'get_term', $categories );
}
return $categories;
}
That function will return any categories that are attached to a published post of a given post type:
$categories = wpse_330111_get_categories_for_post_type( 'team_member' );
get_categories()
? That's the correct function, and what you have in your code, but you mentionget_the_categories()
throughout your question. – Jacob Peattie Commented Feb 27, 2019 at 12:24