Skip to content

Get number of posts belonging to a particular category, tag or custom taxonomy.

$taxonomy = "category"; // can be category, post_tag, or custom taxonomy name
// Use any one of the three methods below
// Using Term ID
$term_id = 1;
$term = get_term_by('id', $term_id, $taxonomy);
// Using Term Name
$term_name = 'A Category';
$term = get_term_by('name', $term_name, $taxonomy);
// Using Term Slug
$term_slug = 'a-category';
$term = get_term_by('slug', $term_slug, $taxonomy);
// Fetch the count
echo $term->count;

Categories: .

Comment Feed

One Response

  1. Thanks, i was exactly looking for that! Works fantastic!

    Kind regards,


Some HTML is OK

or, reply to this post via trackback.