WordPress.org

Ready to get started?Download WordPress

Codex

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

Difference between revisions of "Function Reference/get ancestors"

(Added Source File)
(Added return value section and noted applied filter)
Line 17: Line 17:
 
{{Parameter|$object_id|int or string|The ID of the child object}}
 
{{Parameter|$object_id|int or string|The ID of the child object}}
 
{{Parameter|$object_type|string|The name of the object type (page, category) in question}}
 
{{Parameter|$object_type|string|The name of the object type (page, category) in question}}
  +
  +
==Return Values==
  +
; <tt>(array)</tt> : Array of ancestors from lowest to highest in the hierarchy
   
 
== Examples ==
 
== Examples ==
Line 49: Line 52:
 
) %%%
 
) %%%
   
  +
== Notes ==
  +
* Filter: [[Plugin_API/Filter_Reference#Database_Reads|get_ancestors]] is applied to ancestors array before it is returned.
   
 
== Change Log ==
 
== Change Log ==

Revision as of 19:59, 30 March 2012

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.