WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to display the number of posts under each tag?

[Resolved] How to display the number of posts under each tag?

  • Hi guys,

    Does anyone know how to do this? I would like to display the number of posts that are assigned under each tag on my blog. Like:

    Dog (8)
    Cat (3)
    Fish (15)

    and so on…

Viewing 15 replies - 1 through 15 (of 29 total)
  • At least can someone please tell me if this is possible or not. I have been stuck on this thing for the past week now and would prefer to know so I can move on.

    Thanks.

    Sure i’ll bite.

    <?php
    	// Select all the post tag IDs
    	$the_tags = $wpdb->get_col("SELECT term_id
    	FROM $wpdb->term_taxonomy WHERE taxonomy = 'post_tag'" );
    
    	// Loop over each ID, and grab associated data
    	foreach($the_tags as $tag_id) {
    		// Get information on the post tag
    		$post_tag = get_term( $tag_id, 'post_tag' );
    		// Print the tag name and count (how many posts have this tag)
    		echo $post_tag->name.' ( '.$post_tag->count.' )<br />';
    		// Unset the data when it's not needed
    		unset($post_tag);
    	}
    	?>

    Shame wp_tag_cloud doesn’t provide the tag count, else this would have been a little easier.

    The above is a working solution though…. A list of post tags and how many posts have that tag…

    It can be modified for other uses… and there may be an easier way, but noone else offered anything else up, so i hope that helps… 😉

    Thank you so much for biting t31os_!!!

    There’s a little problem though, currently I list my tags alphabetically:

    I have a A-Z navigation at the top of the page and 25 blocks of code as below:

    <div id="a" class="abc_tags">
    <ul>
    <?php
    $tags = get_tags( array('name__like' => "a", 'order' => 'ASC') );
    foreach ( (array) $tags as $tag ) {
    echo '<li><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></li>';
    }
    ?>
    </ul>
    </div>

    Clicking on “A” in the navigation for example brings the person down the page to A tags and clicking K brings them further down the page to the K tags (anchor links). So that’s why I need the code to be inside their own blocks (loops?) like above.

    I tried mixing your code to integrate with mine but after about a thousand fatal errors I turn to you again for help (I’ll learn PHP very soon I promise).

    You code produces a vertical tag cloud that is in no particular order.
    I need it to be in alphabetical order and also for each alphabet tag to be inside it’s own loop/div (I will then reproduce it 26 times for each alphabet).

    Ok well that’s a bit more of a challenge. You left out these crucial details in your first post.

    However…

    This works..

    <?php
        $query_string = '
    		SELECT *,name FROM '.$wpdb->prefix.'term_taxonomy
    		JOIN '.$wpdb->prefix.'terms
    		ON '.$wpdb->prefix.'term_taxonomy.term_id = '.$wpdb->prefix.'terms.term_id
    		WHERE '.$wpdb->prefix.'term_taxonomy.taxonomy = "post_tag"
    		ORDER by  '.$wpdb->prefix.'terms.name ASC
        ';
    	$post_tags = $wpdb->get_results($query_string);
    	?>
    	<div id="a" class="abc_tags">
    		<ul>
    	<?php
    	foreach($post_tags as $key => $tag) {
    		$newletter = substr($tag->name, 0, 1);
    		if($newletter !== $letter && $key != 0) { ?>
    		</ul>
    	</div>
    	<div id="<?php echo strtolower($newletter); ?>" class="abc_tags">
    		<ul>
    	<?php	} $letter = substr($tag->name, 0, 1); ?>
    			<li><a href="<?php echo get_tag_link($tag->term_id); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $tag->name ); ?>"><?php echo $tag->name.' ('.$tag->count.')';?></a></li>
    		<?php
    	}
    		?>
    		</ul>
    	</div>

    Thank you so much t31os_!!!

    It works perfectly and greatly reduced my original code size. As a token of my appreciation I want to give you two free premium wordpress themes and a premium plugin. Email me if you’re interested (fiatlvx@hotmail.com),

    Thanks again.

    No problem, you’re welcome…

    And thanks, i’ll be happy to grab a copy of whatever you’d like to share mate, i’ll drop you an email shortly..

    Hy guys,

    this is very useful, but to display the number of posts under one tag?

    thanks

    Very useful, thanks guys!

    The code suggested by t31os_ works perfectly.

    I was wondering if there’s a straightforward way to display post titles in a comma separated list, rather than the tag count? And for each post title to link to its respective post. So it’s a combination of t31os_‘s solution and the solution posted here:
    http://wordpress.org/support/topic/328118?replies=8

    For example, say I’ve published the following three posts:

    Post title: Old Yeller is a dog
    Tagged: Dog

    Post title: Old Yeller likes steak
    Tags: Dog, Food

    Post title: Garfield will eat anything
    Tags: Cat, Food

    This is the list I want to create:

    Any ideas are very much appreciated!

    @markellison
    You’d essentially be grabbing any post with a tag then right? As you’re grabbing all tags.

    This might work out as quite an intensive query for a site with lots of tags and lots of posts, but it will do the job.

    $tags_and_posts = $wpdb->get_results("
    SELECT tr.object_id,p.post_title,p.guid,t.name
    FROM wp_term_relationships tr
    JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    JOIN wp_posts p ON tr.object_id = p.ID
    JOIN wp_terms t ON tt.term_id = t.term_id
    WHERE tt.taxonomy = 'post_tag'
    AND p.post_status = 'publish'
    AND p.post_type = 'post'
    ");
    $tag_array = array();
    foreach( $tags_and_posts as $unwanted_key => $result ) {
    	$tag_array[$result->name][] = '<a href="'. $result->guid .'">' . $result->post_title . '</a>';
    	//$tag_array[$result->name][] = '<a href="'. get_permalink( $result->object_id ) .'">' . $result->post_title . '</a>';
    }
    print '<ul>';
    foreach( $tag_array as $tag => $post_titles ) {
    	print '<li>' . $tag . ' ( ' . implode( ', ', $post_titles ) . ' )</li>';
    }
    print '</ul>';

    I’ve left a piece of commented code in there to, that will fetch the permalink for each result, but it would work out to be a more intensive query, the guid method is reliable(should be) and avoids the extra lookup, but use whichever suits you (comment/uncomment as required).

    @giuseppem

    this is very useful, but to display the number of posts under one tag?

    Something simple like this should work..

    $the_term = get_term_by( 'name', 'your-tag-name-here', 'post_tag' );
    echo $the_term->count;

    @t31os_

    Thanks so much! I will try this the first chance I get.

    Maybe you could help with a related issue. I would like to use tags that include a comma, the reason being is I’m tagging by peoples’ names in the format, “Lastname, Firstname”. But whenever I try to make tags like “Johnson, Jack” “Kay, Peter” Smith, Alex”, WP splits them in to distinct tags: Alex, Jack, Johnson, Kay, Peter, Smith.

    I have tried a few plugins (Simple Tags, Page Tagger) with no luck.

    Any ideas are very much appreciated!

    This reminds me of a Trac ticket i read regarding terms with commas.
    http://core.trac.wordpress.org/ticket/13606

    However, i have discovered something that does work for introducing commas into post tags.

    Lastname& #44; Firstname

    &# 44;..being a comma… 🙂

    Without the space between the hash and the fours (not sure if forum will convert them).

    @t31os_

    I tried your suggestion about displaying post titles (with permalinks) alongside tags, but I can’t really get it to work. You are correct that I am trying to grab any post with a tag. I want to create a list of all my tags and, in a list beneath each tag, place permalinks/titles of all posts that are tagged with that tag. Additionally, the tag list should be listed in alphabetical order and grouped by letter. That’s a mouthful! But essentially it’s a combination of this solution:

    <?php
        $query_string = '
    		SELECT *,name FROM '.$wpdb->prefix.'term_taxonomy
    		JOIN '.$wpdb->prefix.'terms
    		ON '.$wpdb->prefix.'term_taxonomy.term_id = '.$wpdb->prefix.'terms.term_id
    		WHERE '.$wpdb->prefix.'term_taxonomy.taxonomy = "post_tag"
    		ORDER by  '.$wpdb->prefix.'terms.name ASC
        ';
    	$post_tags = $wpdb->get_results($query_string);
    	?>
    	<div id="a" class="abc_tags">
    		<ul>
    	<?php
    	foreach($post_tags as $key => $tag) {
    		$newletter = substr($tag->name, 0, 1);
    		if($newletter !== $letter && $key != 0) { ?>
    		</ul>
    	</div>
    	<div id="<?php echo strtolower($newletter); ?>" class="abc_tags">
    		<ul>
    	<?php	} $letter = substr($tag->name, 0, 1); ?>
    			<li><a href="<?php echo get_tag_link($tag->term_id); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $tag->name ); ?>"><?php echo $tag->name.' ('.$tag->count.')';?></a></li>
    		<?php
    	}
    		?>
    		</ul>
    	</div>

    and what you proposed here:

    $tags_and_posts = $wpdb->get_results("
    SELECT tr.object_id,p.post_title,p.guid,t.name
    FROM wp_term_relationships tr
    JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    JOIN wp_posts p ON tr.object_id = p.ID
    JOIN wp_terms t ON tt.term_id = t.term_id
    WHERE tt.taxonomy = 'post_tag'
    AND p.post_status = 'publish'
    AND p.post_type = 'post'
    ");
    $tag_array = array();
    foreach( $tags_and_posts as $unwanted_key => $result ) {
    	$tag_array[$result->name][] = '<a href="'. $result->guid .'">' . $result->post_title . '</a>';
    	//$tag_array[$result->name][] = '<a href="'. get_permalink( $result->object_id ) .'">' . $result->post_title . '</a>';
    }
    print '<ul>';
    foreach( $tag_array as $tag => $post_titles ) {
    	print '<li>' . $tag . ' ( ' . implode( ', ', $post_titles ) . ' )</li>';
    }
    print '</ul>';

    Can you help? Major high fives if so!

    Also, if it makes a difference, I am actually tagging WP pages, not posts, by using the Simple Tags plugin.

    Try something like this…

    $tags_and_posts = $wpdb->get_results("
    	SELECT tr.object_id,p.post_title,p.guid,t.name
    	FROM wp_term_relationships tr
    	JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    	JOIN wp_posts p ON tr.object_id = p.ID
    	JOIN wp_terms t ON tt.term_id = t.term_id
    	WHERE tt.taxonomy = 'post_tag'
    	AND p.post_status = 'publish'
    	AND p.post_type = 'post'
    	ORDER by t.name
    ");
    $tag_array = array();
    $letters = array();
    
    foreach( $tags_and_posts as $unwanted_key => $result ) {
    	$tag_array[$result->name][] = '<a href="'. $result->guid .'">' . $result->post_title . '</a>';
    	//$tag_array[$result->name][] = '<a href="'. get_permalink( $result->object_id ) .'">' . $result->post_title . '</a>';
    }
    foreach( $tag_array as $tag => $post_titles ) {
    	$letter = $tag{0};
    	$letters[ $letter ][] =  $tag . ' ( ' . implode( ', ', $post_titles ) . ' )';
    }
    foreach( $letters as $tag_letter => $tags ) {
    	// Create a list for each letter
    	print '<ul><li>' . implode( '</li><li>', $tags ) . '</li></ul>';
    }

    If you don’t need a list per letter you can simply add this to the query as i’ve done in the above.

    ORDER by t.name

    Hope that helps… 🙂

    Thanks man. I just tried that but I get no content displayed on my page at all. Do I need to use any other code, other than what you’ve given in your most recent comment?

    Make sure it’s inside the necessary PHP tags…

    <?php
    
    // THE CODE
    
    ?>

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘[Resolved] How to display the number of posts under each tag?’ is closed to new replies.