Last active
February 6, 2024 17:19
-
-
Save coulterpeterson/af040fc5b9d1d17b5e31aedb8add09bc to your computer and use it in GitHub Desktop.
WordPress Create Family Tree of Page IDs from Given Page ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Takes in a post_id and finds all ancestor/parent pages, and | |
// then finds all child pages. Returns the family tree in an array and notes what level | |
// the current post_id is located in. | |
function get_all_parent_and_child_pages( $post_id ) { | |
$ancestry_array = [ | |
'ancestors_top_to_curr' => [] | |
]; | |
// ===================== | |
// Get the ancestors | |
// ===================== | |
$post_ancestors = get_post_ancestors($post_id); | |
$post_ancestors = array_reverse( $post_ancestors ); | |
$ancestry_array['ancestors_top_to_curr'] = $post_ancestors; | |
$ancestry_array['ancestors_top_to_curr'][] = $post_id; | |
// ================================================ | |
// Build the family tree | |
// ================================================ | |
$topmost_parent_id = 0; | |
$tree_depth = 0; | |
$curr_page_location_from_top = []; | |
if( isset( $ancestry_array['ancestors_top_to_curr'][0] ) ) { | |
$topmost_parent_id = $ancestry_array['ancestors_top_to_curr'][0]; | |
$tree_depth = 1; | |
if( $post_id == $topmost_parent_id ) { | |
$curr_page_location_from_top = [$topmost_parent_id]; | |
} | |
} | |
// Get second-teir list of children | |
$children_ids_to_add = []; | |
$post_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $topmost_parent_id | |
], 'ARRAY_N' ); | |
foreach( $post_children as $child_id => $child_data ) { | |
$children_ids_to_add[$child_id] = []; | |
if( $post_id == $child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $child_id ]; | |
} | |
} | |
// Check each sibling down 5 levels | |
// 1 Down | |
foreach( $children_ids_to_add as $sibling_id => $sibling_data ) { | |
if( $tree_depth < 2 ) { | |
$tree_depth = 2; | |
} | |
$sibling_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $sibling_id | |
], 'ARRAY_N' ); | |
$sibling_children_to_add = []; | |
foreach( $sibling_children as $sibling_child_id => $sibling_child_data ) { | |
$sibling_children_to_add[$sibling_child_id] = []; | |
if( $post_id == $sibling_child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $sibling_id, $sibling_child_id ]; | |
} | |
} | |
$children_ids_to_add[$sibling_id] = $sibling_children_to_add; | |
// 2 Down | |
foreach( $children_ids_to_add[$sibling_id] as $sibling_2_id => $sibling_2_data ) { | |
if( $tree_depth < 3 ) { | |
$tree_depth = 3; | |
} | |
$sibling_2_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $sibling_2_id | |
], 'ARRAY_N' ); | |
$sibling_children_to_add = []; | |
foreach( $sibling_2_children as $sibling_child_id => $sibling_child_data ) { | |
$sibling_children_to_add[$sibling_child_id] = []; | |
if( $post_id == $sibling_child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $sibling_id, $sibling_2_id, $sibling_child_id ]; | |
} | |
} | |
$children_ids_to_add[$sibling_id][$sibling_2_id] = $sibling_children_to_add; | |
// 3 Down | |
foreach( $children_ids_to_add[$sibling_id][$sibling_2_id] as $sibling_3_id => $sibling_3_data ) { | |
if( $tree_depth < 4 ) { | |
$tree_depth = 4; | |
} | |
$sibling_3_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $sibling_3_id | |
], 'ARRAY_N' ); | |
$sibling_children_to_add = []; | |
foreach( $sibling_3_children as $sibling_child_id => $sibling_child_data ) { | |
$sibling_children_to_add[$sibling_child_id] = []; | |
if( $post_id == $sibling_child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $sibling_id, $sibling_2_id, $sibling_3_id, $sibling_child_id ]; | |
} | |
} | |
$children_ids_to_add[$sibling_id][$sibling_2_id][$sibling_3_id] = $sibling_children_to_add; | |
// 4 Down | |
foreach( $children_ids_to_add[$sibling_id][$sibling_2_id][$sibling_3_id] as $sibling_4_id => $sibling_4_data ) { | |
if( $tree_depth < 5 ) { | |
$tree_depth = 5; | |
} | |
$sibling_4_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $sibling_4_id | |
], 'ARRAY_N' ); | |
$sibling_children_to_add = []; | |
foreach( $sibling_4_children as $sibling_child_id => $sibling_child_data ) { | |
$sibling_children_to_add[$sibling_child_id] = []; | |
if( $post_id == $sibling_child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $sibling_id, $sibling_2_id, $sibling_3_id, $sibling_4_id, $sibling_child_id ]; | |
} | |
} | |
$children_ids_to_add[$sibling_id][$sibling_2_id][$sibling_3_id][$sibling_4_id] = $sibling_children_to_add; | |
// 5 Down | |
foreach( $children_ids_to_add[$sibling_id][$sibling_2_id][$sibling_3_id][$sibling_4_id] as $sibling_5_id => $sibling_5_data ) { | |
if( $tree_depth < 6 ) { | |
$tree_depth = 6; | |
} | |
$sibling_5_children = get_children( [ | |
'post_status' => 'published', | |
'post_parent' => $sibling_5_id | |
], 'ARRAY_N' ); | |
$sibling_children_to_add = []; | |
foreach( $sibling_5_children as $sibling_child_id => $sibling_child_data ) { | |
$sibling_children_to_add[$sibling_child_id] = []; | |
if( $post_id == $sibling_child_id ) { | |
$curr_page_location_from_top = [ $topmost_parent_id, $sibling_id, $sibling_2_id, $sibling_3_id, $sibling_4_id, $sibling_5_id, $sibling_child_id ]; | |
} | |
} | |
$children_ids_to_add[$sibling_id][$sibling_2_id][$sibling_3_id][$sibling_4_id][$sibling_5_id] = $sibling_children_to_add; | |
} // End 5 down | |
} // End 4 down | |
} // End 3 down | |
} // End 2 down | |
} // End 1 down | |
$ancestry_array['family_tree'][$topmost_parent_id] = $children_ids_to_add; | |
// ================================================ | |
// Note the current page's pos. in the hierarchy | |
// ================================================ | |
$ancestry_array['curr_page_level_from_zero'] = count( $curr_page_location_from_top ) - 1; | |
$ancestry_array['curr_page_location_from_top'] = $curr_page_location_from_top; | |
$ancestry_array['num_levels'] = $tree_depth; | |
return $ancestry_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment