wppaste
WordPress

get_post_ancestors( int|WP_Post $post ): int[]

Since
2.5.0
Source
wp-includes/post.php:1137
Retrieves the IDs of the ancestors of a post.

Parameters

$postint|WP_Post
Post ID or post object.

Return

int[]
Array of ancestor IDs or empty array if there are none.

Uses · 1

  • get_post()Retrieves post data given a post ID or post object.

Used by · 4

Source

function get_post_ancestors( $post ) {	$post = get_post( $post ); 	if ( ! $post || empty( $post->post_parent ) || $post->post_parent === $post->ID ) {		return array();	} 	$ancestors = array(); 	$id          = $post->post_parent;	$ancestors[] = $id; 	while ( $ancestor = get_post( $id ) ) {		// Loop detection: If the ancestor has been seen before, break.		if ( empty( $ancestor->post_parent ) || $ancestor->post_parent === $post->ID			|| in_array( $ancestor->post_parent, $ancestors, true )		) {			break;		} 		$id          = $ancestor->post_parent;		$ancestors[] = $id;	} 	return $ancestors;}

History

Introduced in 2.5.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.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/post.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.