WordPress.org

Ready to get started?Download WordPress

Forums

How do I store category post count in a variable? (5 posts)

  1. schmackLab
    Member
    Posted 4 years ago #

    I need to get the number of posts from a given category and store it in a variable. I found this in the documentation but I'm not sure to use this to do what I want or if it's even the right direction.

    Thanks for your help!

  2. juxprose2
    Member
    Posted 4 years ago #

    Not sure if using SELECT is best practice, but this will show the number of cats as $numcats

    http://perishablepress.com/press/2006/08/28/display-total-number-of-posts/

  3. schmackLab
    Member
    Posted 4 years ago #

    Unless I'm missing something that only gives you the total count of all posts where I need the count of posts within specific categories.

    Thanks

  4. juxprose2
    Member
    Posted 4 years ago #

    Maybe combining with this to get the category ID

    // get the first category id
    function get_first_category_ID() {
    	$category = get_the_category();
    	return $category[0]->cat_ID;
    }
  5. keesiemeijer
    moderator
    Posted 4 years ago #

    to get the postcount of category 3:

    <?php
    $categories = get_categories('include=3');
    $cat3 =$categories[0]->category_count;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic