Support » Plugins » Best method for post count per category

  • Resolved lonewolfonline


    I’m having a little trouble obtaining a post count for a given category that includes the post count of all child categories. My current code executes a sql query for each parent, then each sub category and sub sub category and so on. Lots of sql!

    Is there an easy way to do this? I’m developing for 2.3 only.

    example (I am after the count for each item)

    Parent 1 (5)
    – Child 1 (1)
    – Child 2 (1)
    – – Sub Child 1 (1)
    – Child 3 (2)

    Sub Child 1 is easy, just select the count for that id, child’s – one sql for each (but we only want to count unique, ie don’t count a post in child 1 twice if its also in child 3) Then it gets complicated for another level.
    To me this is excessive, especially since performance is important to me (I have a blog with 200+ categories all nested together)

    Any help appreciated



Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH


    Just throwing some spitballs your way 😉

    How about using get_categories(‘child_of=x’) to get an array of the categories then just get all the counts from the term_taxonomy table for those categories?



    Worked perfectly, thanks.

    I was too busy working out how to do a complex sql to see the easy solution!


    hi there. i saw your blog and it seems you are using fold category list plugin too.

    would you be kind and post the actual code that you made to make it work? I’m not very good with these things…:)

    disregard that. i have just decided to use this widget & it adds the child’s posts + parent’s post without needing to do CSS editing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Best method for post count per category’ is closed to new replies.