Return to Snippet

Revision: 51152
at September 16, 2011 00:45 by jvandemerwe


Initial Code
/*
  $grandma = the ancestor to check it with
  $include can be used to include grandma herself too
  please pay attention that the GetChildren is calling itself
  recursively
  use it like: $family = $this->GetFamilyTree($id, true) or
  $family = $this->GetFamilyTree($id, false) when you don't want
  grandma herself too
  see: http://codex.wordpress.org/Function_Reference/get_children
  for additional info
  author: J.J. van de Merwe, Enovision GmbH
 */

function GetFamilyTree($grandma = 1, $include = true) {

    $this->family = array();
    if ($include) {
        array_push($this->family, $grandma);
    }
    $this->GetChildren($grandma, $family);

    return $this->family;
}

private function GetChildren($id) {
    $members = get_children(array(
                'post_parent' => $id,
                'post_type' => 'page',
                'numberposts' => -1,
                'post_status' => 'publish'
            ));
    foreach ($members as $member) {
        array_push($this->family, $member->ID);
        $this->GetChildren($member->ID);
    }
}

Initial URL


Initial Description
This small snippet can be used to get the children and grand children of a Wordpress page. See the comments for how it works

Initial Title
How to get the family id's of a Wordpress page

Initial Tags
wordpress

Initial Language
PHP