get_ancestors( int $object_id = 0, string $object_type = '', string $resource_type = '' ): int[]
- Since
- 3.1.0, 4.1.0
- Source
wp-includes/taxonomy.php:5036
Compatibility
- WordPress
- since 4.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$object_idintoptional- The ID of the object. Default 0.Default:
0 $object_typestringoptional- The type of object for which we'll be retrieving ancestors. Accepts a post type or a taxonomy name. Default empty.Default:
'' $resource_typestringoptional- Type of resource $object_type is. Accepts 'post_type' or 'taxonomy'. Default empty.Default:
''
Return value
int[]- An array of IDs of ancestors from lowest to highest in the hierarchy.
Performance profile
How much work a call to get_ancestors() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 16–45
- Plugin surface
- 1 hook
- Called by
- 5
Reaches the database via get_term().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
Third-party callbacks on 'get_ancestors' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_term()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_ancestors() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 16–21 | apply_filters() |
!empty($object_id) && $resource_type !== "taxonomy" && $resource_type === "post_type" | 25 | get_post_ancestors(), apply_filters() |
!empty($object_id) && is_taxonomy_hierarchical() && $resource_type !== "taxonomy" && $resource_type !== "post_type" | 27 | is_taxonomy_hierarchical(), apply_filters() |
!empty($object_id) && $resource_type === "taxonomy" | 29–36 | get_term(), is_wp_error(), apply_filters() |
!empty($object_id) && !is_taxonomy_hierarchical() && $resource_type !== "taxonomy" && $resource_type !== "post_type" | 29–30 | is_taxonomy_hierarchical(), post_type_exists(), apply_filters() |
!empty($object_id) && is_taxonomy_hierarchical() && $resource_type !== "taxonomy" && $resource_type === "post_type" | 31 | is_taxonomy_hierarchical(), get_post_ancestors(), apply_filters() |
!empty($object_id) && !is_taxonomy_hierarchical() && $resource_type !== "taxonomy" && $resource_type === "post_type" | 33–34 | is_taxonomy_hierarchical(), post_type_exists(), get_post_ancestors(), apply_filters() |
!empty($object_id) && is_taxonomy_hierarchical() && $resource_type === "taxonomy" | 35–42 | is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters() |
!empty($object_id) && !is_taxonomy_hierarchical() && $resource_type === "taxonomy" | 37–45 | is_taxonomy_hierarchical(), post_type_exists(), get_term(), is_wp_error(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 72 | 16–45 | 9 | |
| 8.5 | 72 | 16–45 | 9 | |
| 8.4 | 72 | 16–45 | 9 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 75 | 16–48 | 9 | |
| 8.2 | 75 | 16–48 | 9 | 1 more instruction than PHP 8.1 |
| 8.1 | 74 | 16–53 | 9 | |
| 7.4 | 74 | 16–53 | 9 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while get_ancestors() runs, in this order:
- apply_filters( get_ancestors )filterline 5076 (+40 into the body)
Filters a given object's ancestors.
Uses · 6
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- post_type_exists()Determines whether a post type is registered.
- get_term()Gets all term data from database by term ID.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_post_ancestors()Retrieves the IDs of the ancestors of a post.
Used by · 5
- WP_Terms_List_Table::single_row()
- block_core_breadcrumbs_get_term_ancestors_items()Generates breadcrumb items for hierarchical term ancestors.
- get_term_link()Generates a permalink for a taxonomy term archive.
- get_term_parents_list()Retrieves term parents with separator.
- wp_ajax_add_tag()Handles adding a tag via AJAX.
Source code
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 );}Changelog
Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$resource_type argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.