We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Category Count

Development and Programming

Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

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

       
Jort_K's avatar
Jort_K
13 posts
16 years ago
Jort_K's avatar Jort_K

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!

       
ron00nor's avatar
ron00nor
52 posts
16 years ago
ron00nor's avatar ron00nor

Nice plugin. 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?

       
gridonic's avatar
gridonic
231 posts
16 years ago
gridonic's avatar gridonic
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.

       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

@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 😊

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

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

       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

Hey Mark,

Thanks for the suggestions. If you aren’t an expert, that makes me even less of one! I haven’t used IN before, but i will check it out.

Thanks for the suggestion! Zac

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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.

       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

@Laisvunas - Ha! That makes the second plugin I worked on that you had already developed in a more robust fashion 😊 I guess that means I’m going in the right direction.

Thanks for tuning me in.

Cheers, Zac

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
Hey Mark, Thanks for the suggestions. If you aren’t an expert, that makes me even less of one! I haven’t used IN before, but i will check it out. Thanks for the suggestion! Zac

Let me know what you find out 😉

       
plasticated's avatar
plasticated
15 posts
16 years ago
plasticated's avatar plasticated

Thank you so much. One of those simple features you expect to be in the original code. Relieved to find someone who has taken the time to not only create it, but share it.

       
code72's avatar
code72
104 posts
15 years ago
code72's avatar code72

Works great. Thanks Zac!

       
Mogra's avatar
Mogra
19 posts
15 years ago
Mogra's avatar Mogra

cancelled! wrong reply!

       
Zac G.'s avatar
Zac G.
268 posts
15 years ago
Zac G.'s avatar Zac G.

This is just a notice that you can now find an EE2 version of the plugin in the original post 😊

       
Jim Pannell's avatar
Jim Pannell
187 posts
15 years ago
Jim Pannell's avatar Jim Pannell

Great plugin Zac!

How would I find the number of uncategorised entries using your plugin?

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.