get_term_children

The timeline below displays how wordpress function get_term_children has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Merges all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $term_id  ID of term to get children.
 * @param string $taxonomy Taxonomy name.
 * @return array|WP_Error List of term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
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;
}

WordPress Version: 5.9

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $term_id  ID of term to get children.
 * @param string $taxonomy Taxonomy name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
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;
}

WordPress Version: 5.6

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
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;
}

WordPress Version: 5.3

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $term_id = intval($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;
}

WordPress Version: 4.9

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param int    $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $term_id = intval($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;
}

WordPress Version: 4.6

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy.'));
    }
    $term_id = intval($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;
}

WordPress Version: 4.3

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    }
    $term_id = intval($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;
}

WordPress Version: 4.1

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $term_id ID of Term to get children
 * @param string $taxonomy Taxonomy Name
 * @return array|WP_Error List of Term IDs. WP_Error returned if $taxonomy does not exist
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    }
    $term_id = intval($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;
}

WordPress Version: 3.9

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @uses $wpdb
 * @uses _get_term_hierarchy()
 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
 *
 * @param string $term_id ID of Term to get children
 * @param string $taxonomy Taxonomy Name
 * @return array|WP_Error List of Term IDs. WP_Error returned if $taxonomy does not exist
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    }
    $term_id = intval($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;
}

WordPress Version: 3.7

/**
 * Merge all term children into a single array of their IDs.
 *
 * 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.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses $wpdb
 * @uses _get_term_hierarchy()
 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
 *
 * @param string $term_id ID of Term to get children
 * @param string $taxonomy Taxonomy Name
 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
 */
function get_term_children($term_id, $taxonomy)
{
    if (!taxonomy_exists($taxonomy)) {
        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    }
    $term_id = intval($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 (isset($terms[$child])) {
            $children = array_merge($children, get_term_children($child, $taxonomy));
        }
    }
    return $children;
}