wppaste
WordPress

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:4966
Gets an array of ancestor IDs for a given object.

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

int[]
An array of IDs of ancestors from lowest to highest in the hierarchy.

Hooks fired · 2

2 hooks fire while get_ancestors() runs, in this order:

  1. apply_filters( get_ancestors )filterline 4974 (+8 into the body)

    Filters a given object's ancestors.

  2. apply_filters( get_ancestors )filterline 5006 (+40 into the body)

    Filters a given object's ancestors.

Uses · 6

Used by · 4

Source

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 );}

History

Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.1.0
Introduced the $resource_type argument.from the docblock
3.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.