WordPress.org

Ready to get started?Download WordPress

Codex

Attention 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 ); ?> 

Default Usage

 <?php get_ancestors(
    
'object_id'   => 6
    
'object_type'  => 'page' ); ?> 

Parameters

$object_id
(int or string) (required) The ID of the child object
Default: None
$object_type
(string) (required) The name of the object type (page, category) in question
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.