Get count for all terms inside a parent term

I am trying to get the post count for a term and it's child terms. I tried it with pad_count= true but this seems to have no effect.

My term stucture is like the following:

- term 1: 10 posts
- term 2: 1 post
  -- subterm 1: 4 posts
  -- subterm 2: 12 posts
- term 3: 4 posts

This is my code:

$args = array(
  'pad_counts' = true
);
$terms = get_terms('category', $args);
foreach($terms as $term) {
  var_dump( $term-count );
}

For all the terms without children this works just fine, but for the "term 2", I want to get the counts of itself and it's children combined. In my example this would be 17. The code only gives me 1, though.

Any Tips?

Topic children terms hierarchical Wordpress

Category Web


I don't know why that code didn't work for you. I copy that code and it work exactly as I expected. You should double check your case, did you unset $args variable before, or you can use another name ($agrs2 for example).

Another problem in you question is: if pad_counts doesn't work, you will get 1 not 3.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.