Title: get_term_children
Published: April 25, 2014
Last modified: May 20, 2026

---

# get_term_children( int $term_id, string $taxonomy ): array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#user-contributed-notes)

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

Merges all term children into a single array of their IDs.

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

This recursive function will merge all of the children of $term into the same array
of term IDs. Only useful for taxonomies which are hierarchical.

Will return an empty array if $term does not exist in $taxonomy.

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

 `$term_id`intrequired

ID of term to get children.

`$taxonomy`stringrequired

Taxonomy name.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#return)󠁿

 array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) List
of term IDs. [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
returned if `$taxonomy` does not exist.

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

    ```php
    function get_term_children( $term_id, $taxonomy ) {
    	if ( ! taxonomy_exists( $taxonomy ) ) {
    		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
    	}

    	$term_id = (int) $term_id;

    	$terms = _get_term_hierarchy( $taxonomy );

    	if ( ! isset( $terms[ $term_id ] ) ) {
    		return array();
    	}

    	$children = $terms[ $term_id ];

    	foreach ( (array) $terms[ $term_id ] as $child ) {
    		if ( $term_id === $child ) {
    			continue;
    		}

    		if ( isset( $terms[ $child ] ) ) {
    			$children = array_merge( $children, get_term_children( $child, $taxonomy ) );
    		}
    	}

    	return $children;
    }
    ```

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

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

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

Retrieves children of taxonomy as term IDs.

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

Merges all term children into a single array of their IDs.

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

Determines whether the taxonomy name exists.

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

Retrieves the translation of $text.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_term_children/?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.

  | 
| [WP_Tax_Query::clean_query()](https://developer.wordpress.org/reference/classes/wp_tax_query/clean_query/)`wp-includes/class-wp-tax-query.php` |

Validates a single query.

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

Merges all term children into a single array of their IDs.

  |

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#comment-content-579)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_term_children/#comment-579)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-579)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-579)
 4.  **A Basic Example**
      Used to get an array of children taxonomies and write them
     out with links in an unordered list.
 5.      ```php
         <?php
         $term_id = 10;
         $taxonomy_name = 'products';
         $termchildren = get_term_children( $term_id, $taxonomy_name );
     
         echo '<ul>';
         foreach ( $termchildren as $child ) {
         	$term = get_term_by( 'id', $child, $taxonomy_name );
         	echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
         }
         echo '</ul>';
         ?> 
         ```
     
 6.  This would return something like.
 7.      ```php
         <ul> 
         <li><a href="link_to_term_page">Term 1</a></li>
         <li><a href="link_to_term_page">Term 2</a></li>
         </ul>
         ```
     
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%3Freplytocom%3D579%23feedback-editor-579)
 9.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#comment-content-6104)
 10.   [jon](https://profiles.wordpress.org/adiant/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/get_term_children/#comment-6104)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-6104)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-6104)
 12. Categories and Tags are the two pre-defined Taxonomies. The Taxonomy Name, the
     second required parameter $taxonomy, is ‘category’ for Categories and ‘post_tag’
     for Tags.
 13. If replacing the deprecated function [get_category_children()](https://developer.wordpress.org/reference/functions/get_category_children/),
     which returns a String, note that [get_term_children()](https://developer.wordpress.org/reference/functions/get_term_children/)
     returns an array of Category IDs if the second parameter $taxonomy is ‘category’.
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%3Freplytocom%3D6104%23feedback-editor-6104)
 15.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/get_term_children/?output_format=md#comment-content-6491)
 16.   [Tunn](https://profiles.wordpress.org/iuriem/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/get_term_children/#comment-6491)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-6491)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%23comment-6491)
 18. If the term exists in the taxonomy, but has no children, the term ID will be returned
     instead.
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_term_children%2F%3Freplytocom%3D6491%23feedback-editor-6491)

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