Ready to get started?Download WordPress


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

  1. schmackLab
    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
    Posted 4 years ago #

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


  3. schmackLab
    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.


  4. juxprose2
    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
    Posted 4 years ago #

    to get the postcount of category 3:

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

Topic Closed

This topic has been closed to new replies.

About this Topic