Please post any suggestions, feedback, or bug reports in the comments section of this page.
Download from WordPress.orgAdds 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
- Download the latest zip file and extract the
post-count-per-categorydirectory. - Upload this directory inside your
/wp-content/plugins/directory. - Activate 'Post Count per Category' on the 'Plugins' menu in WordPress.
- 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
IDorslugby separating the values with commas. You have to use eitherIDorslug, not a combination of both. Also, you cannot use thenameattribute (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

Some examples of what you can do with the plugin (in the body of a post).

The code that created the output in the picture above.

Hi, can the plugin also count posts for a tag?
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 replacecat="category-slug"withtag="tag-slug". You can also use IDs or names the same way if you specify atype=""argument.Thanks for the good idea!
Would love to have this for custom taxonomy as well…
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
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…
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
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!
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
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_shortcodeaction to the relevant filter (in this case thewidget_textfilter).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:
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.
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.
Including Future Posts
This subpage explains how to include future (scheduled) posts in the count made by this plugin.
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!
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.
Can I use this plugin to count posts that have a specific category AND a specific custom taxonomy value? Thanks!
Is it possible to insert counting of posts in WP 3.5 menu ?
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 thedo_shortcode()function.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!
(Dashboard) » Appearance » Editor
or just append
/wp-admin/theme-editor.phpto your blog url while logged in.How do I get this plugin to work inside a text widget?
To make your Text widgets shortcode aware, you just need to add the
do_shortcode()function to the widget_text filter. Just paste:somewhere in your theme’s functions.php file.