[WordPress API] About the 'count' field of get_terms function
get_terms is a very common function in WordPress. Here is the definition:
get_terms( array|string $args = array(), array $deprecated = '' )
For example:
$res = get_terms(term->taxonomy, array('parent' => term->id, 'hide_empty' => false));
var_dump($res);
The WordPress will print something like this:
So if the 'count' of a term FATHER_TERM is 3, and the term has 2 child terms A and B. The total number of its posts should be: FATHER_TERM->count + A->count + B->count
We can calculate this by using a recursion function like this:
get_terms( array|string $args = array(), array $deprecated = '' )
For example:
$res = get_terms(term->taxonomy, array('parent' => term->id, 'hide_empty' => false));
var_dump($res);
The WordPress will print something like this:
array (size=5) 0 => object(WP_Term)[4319] public 'term_id' => int 15 public 'name' => string 'Copenhagen S' (length=12) public 'slug' => string 'copenhagen-s' (length=12) public 'term_group' => int 0 public 'term_taxonomy_id' => int 15 public 'taxonomy' => string 'property_city' (length=13) public 'description' => string '' (length=0) public 'parent' => int 37 public 'count' => int 3 public 'filter' => string 'raw' (length=3) 1 => object(WP_Term)[4317] public 'term_id' => int 24 public 'name' => string 'Copenhagen SV' (length=13) public 'slug' => string 'copenhagen-sv' (length=13) public 'term_group' => int 0 public 'term_taxonomy_id' => int 24 public 'taxonomy' => string 'property_city' (length=13) public 'description' => string '' (length=0) public 'parent' => int 37 public 'count' => int 0 public 'filter' => string 'raw' (length=3) 2 => object(WP_Term)[4304] public 'term_id' => int 14 public 'name' => string 'Copenhagen V' (length=12) public 'slug' => string 'copenhagen-v' (length=12) public 'term_group' => int 0 public 'term_taxonomy_id' => int 14 public 'taxonomy' => string 'property_city' (length=13) public 'description' => string '' (length=0) public 'parent' => int 37 public 'count' => int 0 public 'filter' => string 'raw' (length=3) 3 => object(WP_Term)[4303] public 'term_id' => int 20 public 'name' => string 'Nordhavn' (length=8) public 'slug' => string 'nordhavn' (length=8) public 'term_group' => int 0 public 'term_taxonomy_id' => int 20 public 'taxonomy' => string 'property_city' (length=13) public 'description' => string '' (length=0) public 'parent' => int 37 public 'count' => int 2 public 'filter' => string 'raw' (length=3) 4 => object(WP_Term)[4301] public 'term_id' => int 22 public 'name' => string 'Valby' (length=5) public 'slug' => string 'valby' (length=5) public 'term_group' => int 0 public 'term_taxonomy_id' => int 22 public 'taxonomy' => string 'property_city' (length=13) public 'description' => string '' (length=0) public 'parent' => int 37 public 'count' => int 0 public 'filter' => string 'raw' (length=3)The field 'count' will be a little confusing as actually it doesn't represent the number of all the posts belonging to the specific term, but the number of posts only directly belonging to it -- which means the posts belonging to its child terms will not be counted in this number.
So if the 'count' of a term FATHER_TERM is 3, and the term has 2 child terms A and B. The total number of its posts should be: FATHER_TERM->count + A->count + B->count
We can calculate this by using a recursion function like this:
function count_posts_by_term($terms) {
$sub_terms = get_terms( $terms->taxonomy, array(
'parent' => $terms->term_id,
'hide_empty' => false,
)
);
if(count($sub_terms) > 0) {
$sum = $terms->count;
foreach ($sub_terms as $sub) {
$sum += count_posts_by_term($sub);
}
return $sum;
} else {
return $terms->count;
}
}
I'm happy to see the considerable subtle element here!. autoblog wordpress website
回复删除