WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get ancestors

Description

Returns an array containing the parents of the given object.

Usage

 <?php get_ancestors$object_id$object_type$resource_type ); ?> 

Default Usage

 <?php $ancestors get_ancestors6'page''post_type' ); ?> 

Parameters

$object_id
(optional) (The ID of the child object) int or string
Default: None
$object_type
(optional) (The name of the object type (page, hierarchical post type, category, or hierarchical taxonomy) in question) string
Default: None
$resource_type
(optional) (The type of resource $object_type is. Accepts 'post_type' or 'taxonomy') string
Default: None

Return Values

(array) 
Array of ancestors from lowest to highest in the hierarchy

Examples

Given the following category hierarchy (with IDs):

  • Books (6)
    • Fiction (23)
      • Mystery (208)
<?php get_ancestors( 208, 'category' ); ?>

returns:  Array
(
    [0] => 23
    [1] => 6


Given the a page hierarchy (with IDs):

  • About (447)
    • Child Page (448)
<?php get_ancestors( 448, 'page' ); ?>

returns:   Array
(
    [0] => 447

Notes

  • Filter: get_ancestors is applied to ancestors array before it is returned.

Change Log

Source File

get_ancestors() is located in wp-includes/taxonomy.php.

Related

Page Tags: get_all_page_ids(), get_ancestors(), get_page(), get_page_link(), get_page_by_path(), get_page_by_title(), get_page_children(), get_page_hierarchy(), get_page_uri(), get_pages(), is_page(), page_uri_index(), wp_list_pages(), wp_page_menu()

See also index of Function Reference and index of Template Tags.