1 of 2
1
Plugin: Category Count
Posted: 07 January 2009 06:59 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

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 smile

Edit Feb. 19, 2010
1.1 - Added status parameter (Jan. 7, 2009)
2.0 - EE2 compatible

File Attachments
pi.catcount-1.1.zip  (File Size: 2KB - Downloads: 547)
pi.catcount-2.0.zip  (File Size: 2KB - Downloads: 579)
 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 07 January 2009 08:33 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  20
Joined  05-06-2007

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="{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!

 Signature 

oh is dat zo?

Profile
 
 
Posted: 07 January 2009 04:17 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  73
Joined  01-19-2008

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?

Profile
 
 
Posted: 07 January 2009 04:40 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  305
Joined  04-12-2008
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.

 Signature 

Designchuchi | Twitter


URL Field Extension
Required Category Extension
DC FreeForm GeoIP Extension
DC Template Manager

Profile
 
 
Posted: 08 January 2009 07:28 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

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

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 08 January 2009 07:32 AM   [ Ignore ]   [ # 5 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  13725
Joined  04-15-2006

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? wink

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! grin

Best wishes,

Mark

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

Profile
 
 
Posted: 08 January 2009 08:25 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

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

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 08 January 2009 10:17 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 08 January 2009 11:05 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

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

Thanks for tuning me in.

Cheers,
Zac

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 08 January 2009 11:28 AM   [ Ignore ]   [ # 9 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  13725
Joined  04-15-2006
Zac G. - 08 January 2009 01:25 PM

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 wink

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

Profile
 
 
Posted: 10 March 2009 06:18 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Avatar
Total Posts:  24
Joined  01-19-2007

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.

Profile
 
 
Posted: 16 October 2009 12:30 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  132
Joined  04-25-2009

Works great.  Thanks Zac!

Profile
 
 
Posted: 13 December 2009 07:51 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  30
Joined  10-29-2007

cancelled!
wrong reply!

Profile
 
 
Posted: 19 February 2010 06:52 PM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

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

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 12 March 2010 06:37 AM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  229
Joined  08-14-2007

Great plugin Zac!

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

 Signature 

Jim Pannell | Six Media Web Development | Copenhagen, Denmark
Twitter: @jimpannell

Profile
 
 
Posted: 12 March 2010 07:41 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

Hey Jim,

Currently it doesn’t do this, but I can add that functionality…

Out of curiosity, are you using it for EE 1.6 or 2.0?

Cheers,
Zac

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
Posted: 12 March 2010 07:48 AM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  229
Joined  08-14-2007

Hey Zac

Currently for 1.6, but it’s looking more and more likely that I’ll be using version 2 soon.

Cheers


Jim

 Signature 

Jim Pannell | Six Media Web Development | Copenhagen, Denmark
Twitter: @jimpannell

Profile
 
 
Posted: 19 March 2010 11:36 AM   [ Ignore ]   [ # 17 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  150
Joined  10-18-2007

Very nice plugin, is there a way to count numers of entries in open category?

Something like this?

{exp:catcount cat_id="{segment_3}" status="open|close"
{segment_3} 
C16 

so without using exp:weblog:categories and :entries?
I need to show something like this “this category has 35 entries”

Maybe there is a better way to do that, but cant remember :(

thanx again

Profile
 
 
Posted: 19 March 2010 11:46 AM   [ Ignore ]   [ # 18 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  378
Joined  10-24-2007

Hi Davor,

Sorry, I might not understand exactly what you mean.  Are you just looking to use the url segment to serve as the category id or were you looking for all open entries across all categories?

Cheers,
Zac

 Signature 

Zac Gordon is a Professional Web Educator and owner of a Washington, DC based ExpressionEngine Development Company

Profile
 
 
   
1 of 2
1