• I have this code, which works good for 1st level subcategories, but which fails to count 2nd level categories. For example if food is category, fruit is subcategory, than apples and bananas which are subcategories in fruits won’t be counted. In my case I get 0 results since in first category I don’t have anything.
    Someone has any idea how to get around this?

    function wp_get_cat_postcount($id) {
    
      $cat = get_category(pl_getcategory_id([$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;
    }
  • The topic ‘Count posts in subcategories’ is closed to new replies.