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


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. /*
  2.   $grandma = the ancestor to check it with
  3.   $include can be used to include grandma herself too
  4.   please pay attention that the GetChildren is calling itself
  5.   recursively
  6.   use it like: $family = $this->GetFamilyTree($id, true) or
  7.   $family = $this->GetFamilyTree($id, false) when you don't want
  8.   grandma herself too
  9.   see: http://codex.wordpress.org/Function_Reference/get_children
  10.   for additional info
  11.   author: J.J. van de Merwe, Enovision GmbH
  12.  */
  13.  
  14. function GetFamilyTree($grandma = 1, $include = true) {
  15.  
  16. $this->family = array();
  17. if ($include) {
  18. array_push($this->family, $grandma);
  19. }
  20. $this->GetChildren($grandma, $family);
  21.  
  22. return $this->family;
  23. }
  24.  
  25. private function GetChildren($id) {
  26. $members = get_children(array(
  27. 'post_parent' => $id,
  28. 'post_type' => 'page',
  29. 'numberposts' => -1,
  30. 'post_status' => 'publish'
  31. ));
  32. foreach ($members as $member) {
  33. array_push($this->family, $member->ID);
  34. $this->GetChildren($member->ID);
  35. }
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.