September 21st, 2008

get_category_count() is a WordPress function to count the posts within a category. It can probably be written in many different ways.
Here is my version of the function and should be placed in functions.php in your theme:

function wt_get_category_count($input = '') {
	global $wpdb;
	if($input == '')
		$category = get_the_category();
		return $category[0]->category_count;
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
		return $wpdb->get_var($SQL);
		$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
		return $wpdb->get_var($SQL);

It can be called in three different ways…

1. Just call the function within The Loop and it will return the count of the first category within the current post.

<?php echo wt_get_category_count(); ?>

2. Use the function with an ID and it will return the count of the first category within the post ID.

<?php echo wt_get_category_count(1); ?>

3. Use the function with a slug / nicename and it will return the count of the first category within the post slug / nicename.

<?php echo wt_get_category_count('hello-world'); ?>

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2012-03-15 - Livecoupon

    It is recommend that use the second one to fetch category count.

  2. 2012-03-06 - bogdan

    is posibile more categori/tags ? … AND $wpdb->terms.slug IN ($input)”;

  3. 2012-02-16 - ralf

    I have the problem with more then 9 articles/category. This function returns only one digit!?
    Could anybody help me to solve this problem?
    Many thanks in advance!

  4. 2012-01-30 - Bo

    Thanks! It worked just the way I was looking for.

  5. 2012-01-24 - Hugo

    All I can say is thanks!

  6. 2011-08-18 - Tumbleweed

    Hello, I am having trouble getting your syntax to work in my functions.php template.

    So, after I place the above code within the functions.php Loop, where do I place the calling script:
    At the end of the Loop in the functions.php template?

    And, will this give me a count of all posts within each category?


  7. 2011-07-31 - sardinien strand

    Nice work, but there is one thing that does not work. It does not display a number or a parent category, when child categories have posts -> it just shows “0″ posts for parent category.

  8. 2011-05-26 - Egzon

    Great tip. Thanks

  9. 2011-03-21 - Krassi

    Very nice function. Thank you very much.

  10. 2011-03-02 - Paul

    This is a great function. Thank you. Is it possible to make this function count all posts in the subcategories of the current category?

  11. 2010-06-29 - Nathan


    I found this to be of great help!


  12. 2009-12-25 - mikeamc

    Very nice tip, but when a category is empty the function do not return the zero “0″. Is there a way to resolve that ? thanks a lot

  13. 2009-08-05 - Dominic Desbiens

    Very nice function. Thank you very much. Is it count all posts in sub-cats when you enter a main cat? (parent cat)

  14. 2009-03-02 - Sam Davis

    I have a website dedicated to Magento and I use this feature on my home page.

    However, is it possible to use this function for Link Categories (ie: The number of Blogroll links in a particular cat)?


  1. درخواست کد تعداد مطالب ارسالی « انجمن پرسش و پاسخ وردپرس
    WordPress获取某个分类下的文章数 _ 缝隙里の世界
    Léo Novais
    WordPress获取特定分类文章数. | Predu