Are you looking for a way to count all posts in category and display it in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to count all posts in a category in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function count_cat_post($category) { if(is_string($category)) { $catID = get_cat_ID($category); } elseif(is_numeric($category)) { $catID = $category; } else { return 0; } $cat = get_category($catID); return $cat->count; }
You can use this code to display the posts count from a category anywhere on your website either by category ID or category name:
<?php // echo count_cat_post('1'); // echo count_cat_post('General'); ?>
Simply remove ‘//’ from any of the code line to use it.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 15 best content marketing tools and how to create a contact form in WordPress.
Hi,
How can i use the same function but with the ‘AND’ relation.
only posts that have both categories will return.
How can we pass the id or name dynamically?
You can pass a variable to the function. For example, you could pass the first returned category id of the
get_the_category()
function: https://developer.wordpress.org/reference/functions/get_the_category/Note that this requires working with arrays and objects, as the returned value from
get_the_category()
is an array of objects.For example, you could use:
$cat = get_the_category();
$cat_id = $cat[0]->cat_ID;//Gets the first id
With the latest version of WP, there were some bugs with all of this working right out of the box, so if anyone else has trouble:
function count_cat_post($catslug){
$CategoryTerms = get_term_by('slug', $catslug, 'category');
if(!$CategoryTerms){
return 0;
}
return $CategoryTerms->count;
}
Hi Jason,
Thank you for this. However, we were able to test on WordPress 5.4 and it is working for us without errors.
ALSO,
use get_the_category since get_category will error out with the latest versions of wordpress on anything that has posts and return 0 on the ones that don’t, causing a little bit of a troubleshooting conundrum.
We are not seeing this. What is the error message you are getting?
also it:
return $cat->category_count;
since count is a php function.
$cat
is aWP_Term
object, withcount
being an public int property.is the any way to not put category name or id? i have so many categories. cant come to code and add each ones id or name in the code. need wordpress to do it automaticly.
In your template there will be an area where it is outputting the category name, there is likely a part of that that uses a variable that is equal to the category id or slug, that you can use instead to pull the count. Something that ends up like:
// echo count_cat_post($cat_id);
You could use a
for
loop to cycle through the categories. You may want to useget_categories()
in this case: https://developer.wordpress.org/reference/functions/get_categories/