As I was working on this plugin, I kept thinking: I really hope that this is a core function that I just not finding. This plugin does one simple thing. It returns the number of entries associated with a given category.
This is how it works:
{exp:catcount cat_id="33" status="open|close"}
This would output a number, such as “12” where you place it.
It can also work nested within the exp:weblog:categories like so:
{exp:weblog:categories weblog="{current-weblog}"}
<li>{category_name}({exp:catcount cat_id="{category_id}"})</li>
{/exp:weblog:categories}
Please let me know if there is a core function that does this, and if not… enjoy 😊
Edit Feb. 19, 2010 1.1 - Added status parameter (Jan. 7, 2009) 2.0 - EE2 compatible
That’s great. Although, I currently use the EE Query module to count the entries in a category. But you need a personal or commercial license of EE to get that worked out. EE Query module isn’t included in the free core edition.
{exp:weblog:categories weblog="{my_weblog}"}
{exp:query sql="SELECT count(exp_category_posts.entry_id) AS post_count FROM exp_category_posts WHERE exp_category_posts.cat_id = {category_id} "}
<a href="http://{path={my_template_group}/index}">{category_name}</a> ({post_count})
{/exp:query}
{/exp:weblog:categories}
So with this plugin, users of the free core edition can show the number of items per category, nice one!
SELECT count(exp_category_posts.entry_id) AS post_count FROM exp_category_posts WHERE exp_category_posts.cat_id = {category_id}
This SQL query will show all entries for that category, even if they might have a status where the entry is not visible to the user. In order to check for the status, you’d have to JOIN the category posts with at least one more table.
Just a thought, this is how we do it on our website where we don’t want categories for closed entries (or not open) be included in the count.
@ron00nor
There’s one disadvantage on both solutions: they also count the closed and other statuses. Maybe adjust the query so that only open entries are selected?
That was a great call! I updated the plugin to accept status as a parameter. It defaults to just entries with status=”open”
You can get the uploaded version in the original post.
Thanks for the idea 😊
Perhaps a quick update to the first post to let people know that they can now use the status=”closed|featured” parameter in the plugin tag? 😉
Also I’m not fantastic with SQL queries but just wondering if there are a lot of statuses would the IN(‘closed’, ‘featured’) SQL query work better than lots of ANDs.
As I say I know just enough SQL to put together a query so not sure on that one?
Thanks for a great little plugin by the way! 😊
Best wishes,
Mark
Hi Zac,
My plugin Entries Number can achieve the same as yours - to output the number of entries posted into some category and having certain status.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.