WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Best method for post count per category (5 posts)

  1. lonewolfonline
    Member
    Posted 6 years ago #

    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

    Thanks

    Tim

  2. MichaelH
    Member
    Posted 6 years ago #

    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?

  3. lonewolfonline
    Member
    Posted 6 years ago #

    Worked perfectly, thanks.

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

    Cheers

  4. intercostalspace
    Member
    Posted 6 years ago #

    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...:)

  5. intercostalspace
    Member
    Posted 6 years ago #

    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. http://wordpress.org/extend/plugins/folding-category-widget/

Topic Closed

This topic has been closed to new replies.

About this Topic