2143 | | function &get_page_children($page_id, $pages) { |
2144 | | $page_list = array(); |
2145 | | foreach ( (array) $pages as $page ) { |
2146 | | if ( $page->post_parent == $page_id ) { |
2147 | | $page_list[] = $page; |
2148 | | if ( $children = get_page_children($page->ID, $pages) ) |
2149 | | $page_list = array_merge($page_list, $children); |
| 2140 | function &get_page_children( $page_id, &$pages ) { |
| 2141 | |
| 2142 | /* |
| 2143 | * use auxiliary array structure to store the parent-children relations, |
| 2144 | * which allow O(1) per lookup, and gives O(N) overall complexity |
| 2145 | * ex: children[10][] contains all the children pages whose parent is 10 |
| 2146 | */ |
| 2147 | $children = array(); |
| 2148 | foreach ( (array) $pages as $p ) { |
| 2149 | |
| 2150 | $parent_id = intval( $p->post_parent ); |
| 2151 | $children[ $parent_id ][] = $p; |
| 2152 | } |
| 2153 | |
| 2154 | $page_list = array(); |
| 2155 | _page_traverse( $page_id, $children, $page_list ); |
| 2156 | |
| 2157 | return $page_list; |
| 2158 | } |
| 2159 | |
| 2160 | /** |
| 2161 | * function to traverse and return all the nested children of a root page. |
| 2162 | * $children contains parent-chilren relations |
| 2163 | * |
| 2164 | */ |
| 2165 | function _page_traverse( $page_id, &$children, &$page_list ){ |
| 2166 | |
| 2167 | if ( isset( $children[ $page_id ] ) ){ |
| 2168 | |
| 2169 | foreach( (array)$children[ $page_id ] as $child ) { |
| 2170 | |
| 2171 | $page_list[] = $child; |
| 2172 | _page_traverse( $child->ID, $children, $page_list ); |