Count Posts in a Category, Tag, or Custom Taxonomy

Adds a custom shortcode that returns the number of posts in a category, tag, or custom taxonomy. Accepts a slug (default), ID, or name as input and works on all terms.

Description

This plugin allows you to dynamically return the number of posts in a particular category, tag, or custom taxonomy. Inserting [cat_count value="category-slug"] in a post or page, or <?php do_shortcode('cat_count slug="category-slug" '); ?> anywhere in WordPress' code will return the number of posts in that particular category.

Useful for creating dynamic table of contents pages, keeping track of post stats, general bragging, or any place where keeping a running tally might be desirable.

Installation

  1. Download the latest zip file and extract the post-count-per-category directory.
  2. Upload this directory inside your /wp-content/plugins/ directory.
  3. Activate 'Post Count per Category' on the 'Plugins' menu in WordPress.
  4. Insert in a post or page: [cat_count slug="category-slug"] to count a category, [tag_count slug="tag-slug"] to count a tag, or [tax_count tax="taxonomy-name" slug="taxonomy-slug"] to count a custom taxonomy. (Replacing the slugs in quotes with an acutal slug name) to return the number of posts in that category, tag or custom taxonomy.

Frequently Asked Questions

How do I add it into my posts?

You must insert the shortcode in location where you want to count to appear: (always replacing category-slug with an acutal slug name)

For categories: * [cat_count slug="category-slug"]

or for a tag: * [tag_count slug="tag-slug"]

or for a custom taxonomy: * [tax_count tax="taxonomy-name" slug="taxonomy-slug"]

The legacy shortcode assumes you are giving it slugs, but it can also accept category names or IDs:

  • [ccp type="name" cat="Category Name"]
  • [ccp type="id" cat="27"]

or for a tag: * [ccp type="name" tag="Tag Name"] * [ccp type="id" tag="28"]

With the new shortcodes, you can specify slug="", id="", or name="" directly. If more than one is given, posts are selected in that order.

Can I add the count multiple terms?

  • As of version 3.1... Yes! You can specify more than 1 ID or slug by separating the values with commas. You have to use either ID or slug, not a combination of both. Also, you cannot use the name attribute (since a name may ostensibly contain commas naturally, unlike a slug or ID).

  • So, for example, if you had 3 categories with the slugs "cat-1", "cat-2", and "cat-3" each with 10 posts, you could get the count for all three (it would return "30") using this shortcode:

[cat_count slug="cat-1,cat-2,cat-3"]

  • Thanks to Jack Brand for this excellent suggestion.

It keeps returning 0 posts, why?

Make sure you're using the correct slug, sometimes they are different from the actual category name and capitalization matters. You can view them by clicking on the Categories link under Posts.

Also don't literally copy "category-slug," you need to put the category you want counted there, that's just a place holder. So to count the number of posts in a category called 'Book Reviews' with a slug name of 'book-reviews' you would insert [cat_count slug="book-reviews"].

Why are there 2 kinds of shortcodes

The shortcodes without underscores are legacy shortcodes, I left them in the plugin so stuff won't break, but you should use the new shortcodes if possible. The legacy shortcodes include: [ccp] [catcountposts] [countposts] [countpost] [postsincat] [postincat]

The legacy shortcodes only work for categories and tags, and they all follow the format [ccp type="slug/id/name" cat="category-slug/id/name" tag="tag-slug/id/name"]

The NEW shortcodes all have underscores, and follow a slightly different format. There are 3 seperate shortcodes: For Categories: * [cat_count slug="cat-slug"] or [cat_count id="45"] or [cat_count name="Category Name"]

For Tags: * [tag_count {slug="tag-slug"} {id="45"} {name="tag name"} ]

For Custom Taxonomies: * [tax_count tax="custom-tax-type" {slug="tax-slug"} {id="99"} {name="Taxonomy Item Name"} ]

You only need to specify 1 of the following: slug="", id="", or name="". If you specify more than one, the plugin will ignore the other attributes in the order of slug > id > name.

You also need to specify the tax="custom-tax-type" for the [tax_count] shortcode. This is so the plugin knows which custom taxonomy to count.

Can you add this feature I just thought of?

Can I? Yes. Will I? Yes, if I think it would be a helpful addition. I'm trying to keep things clean and simple, but there's always room for improvement, so let me know if you think a feature is lacking!

Screenshots

Count Posts in a Category, Tag, or Custom Taxonomy Screenshot 1
Some examples of what you can do with the plugin (in the body of a post).

Count Posts in a Category, Tag, or Custom Taxonomy Screenshot 2
The code that created the output in the picture above.

30 Thoughts on “Count Posts in a Category, Tag, or Custom Taxonomy

  1. redevolve on October 1, 2012 at 7:31 am said:

    Hi, can the plugin also count posts for a tag?

    • bitacre on October 2, 2012 at 11:36 pm said:

      It can now, I just pushed a new version (2.1) which has the tag="" argument. So you would use it just like the regular shortcode except you would replace cat="category-slug" with tag="tag-slug". You can also use IDs or names the same way if you specify a type="" argument.

      Thanks for the good idea!

      • Would love to have this for custom taxonomy as well…

        • bitacre on October 3, 2012 at 2:54 pm said:

          Haha, you guys are tough customers! This is easily doable, but I think the shortcode is going to get too confusing if I add any more attributes. I’ll tell you what, I’ll preserve the current functionality so nothing breaks, but I’ll add some additional shortcodes to make it less confusing. I will add

          [term_count taxonomy="custom-taxonomy-name" term="tax-slug/name/id" type="slug/name/id"]

          Because this will require the additional paramater of taxonomy=”custom-taxonomy-name”, I will put it in a separate shortcode.

          Or maybe I should just make a new one altogether…

          • adkhan on July 7, 2013 at 9:44 pm said:

            Hi, i wanna know how would you write the following shortcode in php

            [term_count taxonomy="custom-taxonomy-name" term="tax-slug/name/id" type="slug/name/id"]

  2. Hi,

    As we talked about via mail;

    It would be great to have a sum of all posts for a specific category, f.e. [tag_count id="45,85,34"] would give you a sum of all the posts in those categories.

    Thanks

    • bitacre on October 26, 2012 at 3:36 pm said:

      This feature is added as of version 3.1. It only works for IDs and slugs (since a name could ostensibly have a naturally occurring comma).

      Thanks for the great idea, I gave you credit in the readme.txt! :)

  3. I thought I had this working for me in a widget, but after deleting some other plugins today it no longer works. I can get the code to work for me inside a post, but not inside a widget. Should it be able to do both?

    Thanks

    • bitacre on October 26, 2012 at 3:07 pm said:

      Are you inserting the PHP function <?php do_shortcode( 'cat_count id="44"' ); ?> directly into the widget’s code; or are you just typing[cat_count id="44"] into a widget like you would in a post?

      By default, NO shortcodes will work inside of widgets or in comments. To make shortcodes work outside of posts and pages, you need to add the do_shortcode action to the relevant filter (in this case the widget_text filter).

      So either add the above listed PHP into your widget, or add this line to make ALL shortcodes work inside of the body of the text widget:

      add_filter( 'widget_text', 'do_shortcode' );

      • Awesome – thanks for the taking the time. Worked by adding the line into the theme functions and then [cat_count name="cat_name"] into the widget, though I couldn’t get the php line in the widget to render anything.

        • bitacre on November 10, 2012 at 3:10 pm said:

          You still won’t be able to paste PHP directly into the widget, just shortcodes, if that’s what you’re talking about.

          If you need to put PHP into the widget text, you’ll have to use your own callback function and hook it to the widget_text filter.

  4. bitacre on October 26, 2012 at 5:16 pm said:

    Including Future Posts

    This subpage explains how to include future (scheduled) posts in the count made by this plugin.

  5. hi, does this also work on custom post-types?

    im trying to get it to work for a custom post type categories, but it returns 0

    please help!

    • bitacre on November 29, 2012 at 9:34 pm said:

      Yes it does! You have to use the format

      [tax_count tax="taxonomy-name" slug="taxonomy-slug"]

      And replace the “taxonomy-name” with your custom category name and the “taxonomy-slug” with the slug of that custom category.

  6. Can I use this plugin to count posts that have a specific category AND a specific custom taxonomy value? Thanks!

  7. Labs Cilvēks on January 14, 2013 at 5:07 am said:

    Is it possible to insert counting of posts in WP 3.5 menu ?

    • bitacre on January 16, 2013 at 11:43 am said:

      You’ll have to enable the shortcode filter for menus in your theme, just wrap any section you want to be shortcode aware in the PHP code below. If you want it in your navigation menus, the simplest way is just to find wp_nav_menu( $args ) function wherever that appears in your theme (it may have an array inside of it or variable other than $args) and wrap it in the do_shortcode() function.

      < ?php do_shortcode( wp_nav_menu( $args ) ); ?>

      • rene on March 1, 2013 at 1:29 pm said:

        how to continue after I put the code < ?php do_shortcode( wp_nav_menu( $args ) ); ?>… Im lost and pulling my hair..

        sorry for maybe being dumb..

        thnks a lot!

        • bitacre on March 3, 2013 at 9:33 am said:

          (Dashboard) » Appearance » Editor

          or just append /wp-admin/theme-editor.php to your blog url while logged in.

  8. How do I get this plugin to work inside a text widget?

    • bitacre on January 25, 2013 at 11:48 am said:

      To make your Text widgets shortcode aware, you just need to add the do_shortcode() function to the widget_text filter. Just paste:

      add_filter( 'widget_text', 'do_shortcode' );

      somewhere in your theme’s functions.php file.

  9. Hombo on July 17, 2013 at 6:26 am said:

    Hi Luke,

    on the wordpress support forum for your plugin … here:

    http://wordpress.org/support/topic/plugin-count-posts-in-a-category-please-post-support-questions-to-the-plugins-homepage?replies=1

    you are posting a link to your support page… unfortunately it returns a 404 error.

    Might want to look into that.

    Hope it helps.

  10. Hombo on July 17, 2013 at 6:27 am said:

    I am not sure if this is implemented, if not please take this as a feature request.

    Can I “query” different categories with AND or OR relationsships?

    I see that you can do e.g.: [cat_count slug="cat-1,cat-2,cat-3"]

    But can i do: COUNT number of post in category 1 AND 2.. but NOT if they are category 3 ?

    This is something which would come handy for us as we have posts which are in multiple categories and we would like to filter out duplicate counts.

    An example of such “query syntax” can be seen on another plugin we are using ( it displays the posts in certain categories). Please go here: http://wordpress.org/plugins/list-category-posts/other_notes/

    and you can see how the author implemented “Include AND”, “Include OR” and “Exclude” queries. Hope this helps.

    Can this perhaps be done already? if not would be great if you could consider this.

  11. I can’t get this to work. I’m using the shortcode in a post, and it’s returning a “0.”

  12. Danilo on October 4, 2013 at 9:51 pm said:

    Hi. It would be possible to add AND operator (+) for category count?

    Example: I need to count posts only if are presents in CATEGORY 1 [AND] CATEGORY 2. In WordPress, I can use + sign, but if I use with latest version of plugin, I get 0.

    It would be possible to add this feature?

  13. I am using the php code and it wont show up

  14. Had some trouble getting your “do_shortcode” example working, fixed it by changing to

  15. Hello!

    I would like to add a count in my site every catergory menu as You can see in here: http://penyigeydesign.hu/herbertcollection/

    Now it is manualy. Your plugin can help me?

    Sorry for my english!

    Thanks Zoltan

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>