Title: _pad_term_counts
Published: April 25, 2014
Last modified: February 24, 2026

---

# _pad_term_counts( object[]|WP_Term[] $terms, string $taxonomy )

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Adds count of children to parent count.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#description)󠁿

Recalculates term counts by including items from child terms. Assumes all relevant
children are already in the $terms argument.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#parameters)󠁿

 `$terms`object[]|[WP_Term](https://developer.wordpress.org/reference/classes/wp_term/)[]
required

List of term objects (passed by reference).

`$taxonomy`stringrequired

Term context.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#source)󠁿

    ```php
    function _pad_term_counts( &$terms, $taxonomy ) {
    	global $wpdb;

    	// This function only works for hierarchical taxonomies like post categories.
    	if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
    		return;
    	}

    	$term_hier = _get_term_hierarchy( $taxonomy );

    	if ( empty( $term_hier ) ) {
    		return;
    	}

    	$term_items  = array();
    	$terms_by_id = array();
    	$term_ids    = array();

    	foreach ( (array) $terms as $key => $term ) {
    		$terms_by_id[ $term->term_id ]       = & $terms[ $key ];
    		$term_ids[ $term->term_taxonomy_id ] = $term->term_id;
    	}

    	// Get the object and term IDs and stick them in a lookup table.
    	$tax_obj      = get_taxonomy( $taxonomy );
    	$object_types = esc_sql( $tax_obj->object_type );
    	$results      = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" );

    	foreach ( $results as $row ) {
    		$id = $term_ids[ $row->term_taxonomy_id ];

    		$term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1;
    	}

    	// Touch every ancestor's lookup row for each post in each term.
    	foreach ( $term_ids as $term_id ) {
    		$child     = $term_id;
    		$ancestors = array();
    		while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) {
    			$ancestors[] = $child;

    			if ( ! empty( $term_items[ $term_id ] ) ) {
    				foreach ( $term_items[ $term_id ] as $item_id => $touches ) {
    					$term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1;
    				}
    			}

    			$child = $parent;

    			if ( in_array( $parent, $ancestors, true ) ) {
    				break;
    			}
    		}
    	}

    	// Transfer the touched cells.
    	foreach ( (array) $term_items as $id => $items ) {
    		if ( isset( $terms_by_id[ $id ] ) ) {
    			$terms_by_id[ $id ]->count = count( $items );
    		}
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/taxonomy.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/taxonomy.php#L4039)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/taxonomy.php#L4039-L4100)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_sql()](https://developer.wordpress.org/reference/functions/esc_sql/)`wp-includes/formatting.php` |

Escapes data for use in a MySQL query.

  | 
| [_get_term_hierarchy()](https://developer.wordpress.org/reference/functions/_get_term_hierarchy/)`wp-includes/taxonomy.php` |

Retrieves children of taxonomy as term IDs.

  | 
| [is_taxonomy_hierarchical()](https://developer.wordpress.org/reference/functions/is_taxonomy_hierarchical/)`wp-includes/taxonomy.php` |

Determines whether the taxonomy object is hierarchical.

  | 
| [get_taxonomy()](https://developer.wordpress.org/reference/functions/get_taxonomy/)`wp-includes/taxonomy.php` |

Retrieves the taxonomy object of $taxonomy.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#)

| Used by | Description | 
| [WP_Term_Query::get_terms()](https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/)`wp-includes/class-wp-term-query.php` |

Retrieves the query results.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_pad_term_counts/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_pad_term_counts%2F)
before being able to contribute a note or feedback.