The following code currently indexes all tags for my site (whether attached to a standard post or a custom post) like this

I want to limit this code to index tags from only a specific custom post (and then taxonomy within that custom post) but I cannot work out what adjustment I need to the initial code (I don't know how to set the $args and I suspect I cannot use get_terms() for custom posts).

Would appreciate any suggestions.

    $list = '';
$tags = get_terms( 'post_tag' );
$groups = array();
if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
    if( !empty( $groups ) ) {
        foreach( $groups as $letter => $tags ) {
            $list .= "\n" . '<div class="index-letter">' . apply_filters( 'the_title', $letter ) . '</div>';

            foreach( $tags as $tag ) {
                $url = attribute_escape( get_tag_link( $tag->term_id ) );
                $count = intval( $tag->count );
                $name = apply_filters( 'the_title', $tag->name );
                $list .= "\n" . '<li><a href="' . $url . '">' . $name . '</a> (' . $count . ')</li>';
            $list .= "\n" . '</li>';
}else $list .= "\n" . '<p>Sorry, but no tags were found</p>';?>

1 Answer 1


I've been successfully using the following code from @StephenHarris from this answer. I have made a small tweak or two to the original code, but the most significant is to name the new count object to count_type from the original COUNT* that was returned by default

Just in short again, the function works exactly like you would normally use get_terms. There is an extra argument however, called post_type with will accept an array or string of post type names. When the post_type arguments is set, the function uses the terms_clauses filter to get the terms by that specific post type only

NOTE: One big caution when setting a post type that was not mentioned previously, the function returns two count objects with keys count and (now named from COUNT*) count_type. count still retrieves the post count for the specific term across the board, not the count for the specific post type. To display the correct count for the post type given, you will have to make use of count_type which will display the correct count

As example, here is the returned object when a post type is set

object(stdClass)#583 (10) {
  string(3) "145"
  string(7) "testing"
  string(7) "testing"
  string(1) "0"
  string(3) "145"
  string(8) "post_tag"
  string(0) ""
  string(1) "0"
  string(1) "4"
  string(1) "2"

You can see count is 4, this is the total posts from all post types, count_type is 2, this is the amount of posts with the specific tag in the specified post type

Here is the code that goes into your functions.php (Again, special thanks to @StephenHarris for the original code)

function get_terms_per_post_type( $taxonomies, $args=array() ) {
    //Parse $args in case its a query string.
    $args = wp_parse_args($args);

    if( !empty( $args['post_type'] ) ){

        $args['post_type'] = (array)$args['post_type'];

        add_filter( 'terms_clauses', function ( $pieces, $tax, $args){
            global $wpdb;

            //Don't use db count
            $pieces['fields'] .= ", COUNT(*) AS count_type" ;

            //Join extra tables to restrict by post type.
            $pieces['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id 
                                 INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id ";

            //Restrict by post type and Group by term_id for COUNTing.
            $post_type = implode( ',', $args['post_type'] );
            $pieces['where'] .= $wpdb->prepare( " AND p.post_type IN(%s) GROUP BY t.term_id", $post_type );

            remove_filter( current_filter(), __FUNCTION__ );

            return $pieces;

        }, 10, 3 );


    return get_terms($taxonomies, $args);           

You can use it as follows in your template files

$terms = get_terms_per_post_type( 'post_tag', array( 'post_type' => 'post' ) );

foreach ( $terms as $term ) {
    echo "$term->name ( $term->count_type ) </br>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.