categories - Getting used tags per post type

admin2025-06-02  1

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)?

Share Improve this question asked Feb 27, 2019 at 12:05 Jesse OrangeJesse Orange 1131 silver badge7 bronze badges 4
  • Do you mean get_categories()? That's the correct function, and what you have in your code, but you mention get_the_categories() throughout your question. – Jacob Peattie Commented Feb 27, 2019 at 12:24
  • That being said, no, there isn't a way to do what you're asking. Not without just writing raw SQL. It sounds like you'd be better off just using a separate taxonomy. Is that acceptable? – Jacob Peattie Commented Feb 27, 2019 at 12:25
  • So, there's no way to get categories in use per post type? – Jesse Orange Commented Feb 27, 2019 at 12:30
  • Essentially, I just want 3 global categories, but if a post type doesn't have that category, it isn't worth filtering by it. – Jesse Orange Commented Feb 27, 2019 at 12:31
Add a comment  | 

1 Answer 1

Reset to default 2

The 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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748859873a314320.html

最新回复(0)