get_ancestors

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

WordPress Version: 6.1

/**
 * Gets an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors, true)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filters a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param int[]  $ancestors     An array of IDs of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 5.5

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors, true)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filters a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param int[]  $ancestors     An array of IDs of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 5.4

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filters a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param int[]  $ancestors     An array of IDs of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 4.6

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filters a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param array  $ancestors     An array of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 4.3

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filter a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param array  $ancestors     An array of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 4.2

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } elseif (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filter a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.0 Introduced the `$resource_type` parameter.
     *
     * @param array  $ancestors     An array of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 1.1

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } else if (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filter a given object's ancestors.
     *
     * @since 3.1.0
     * @since 4.1.1 Introduced the `$resource_type` parameter.
     *
     * @param array  $ancestors     An array of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
}

WordPress Version: 4.1

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '', $resource_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type, $resource_type);
    }
    if (!$resource_type) {
        if (is_taxonomy_hierarchical($object_type)) {
            $resource_type = 'taxonomy';
        } else if (post_type_exists($object_type)) {
            $resource_type = 'post_type';
        }
    }
    if ('taxonomy' === $resource_type) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif ('post_type' === $resource_type) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filter a given object's ancestors.
     *
     * @since 3.1.0
     *
     * @param array  $ancestors     An array of object ancestors.
     * @param int    $object_id     Object ID.
     * @param string $object_type   Type of object.
     * @param string $resource_type Type of resource $object_type is.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
}

WordPress Version: 3.9

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @param int $object_id The ID of the object
 * @param string $object_type The type of object for which we'll be retrieving ancestors.
 * @return array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        /** This filter is documented in wp-includes/taxonomy.php */
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
    }
    if (is_taxonomy_hierarchical($object_type)) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif (post_type_exists($object_type)) {
        $ancestors = get_post_ancestors($object_id);
    }
    /**
     * Filter a given object's ancestors.
     *
     * @since 3.1.0
     *
     * @param array  $ancestors   An array of object ancestors.
     * @param int    $object_id   Object ID.
     * @param string $object_type Type of object.
     */
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
}

WordPress Version: 3.7

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @param int $object_id The ID of the object
 * @param string $object_type The type of object for which we'll be retrieving ancestors.
 * @return array of ancestors from lowest to highest in the hierarchy.
 */
function get_ancestors($object_id = 0, $object_type = '')
{
    $object_id = (int) $object_id;
    $ancestors = array();
    if (empty($object_id)) {
        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
    }
    if (is_taxonomy_hierarchical($object_type)) {
        $term = get_term($object_id, $object_type);
        while (!is_wp_error($term) && !empty($term->parent) && !in_array($term->parent, $ancestors)) {
            $ancestors[] = (int) $term->parent;
            $term = get_term($term->parent, $object_type);
        }
    } elseif (post_type_exists($object_type)) {
        $ancestors = get_post_ancestors($object_id);
    }
    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
}