Tuesday, February 21, 2012

WordPress how to get the total count of posts from specific category


A custom function which will return total post counts from the specified category and its child categories.
function wp_get_cat_postcount($id) {
    $cat = get_category($id);
    $count = (int) $cat->count;
    $taxonomy = 'category';
    $args = array(
      'child_of' => $id,
    );
    $tax_terms = get_terms($taxonomy,$args);
    foreach ($tax_terms as $tax_term) {
        $count +=$tax_term->count;
    }
    return $count;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.