You have done this the hard way. WP_Query
can do most of the work for you.
function get_all_pages($page_title) {
$page = get_page_by_title($page_title);
if (empty($page)) return array();
$children = new WP_Query(
array(
'post_type' => 'page',
'post_parent' => $page->ID,
'fields' => 'ids'
)
);
array_unshift($children->posts,"{$page->ID}");
return $children->posts;
}
$allpages = get_all_pages('Sample Page');
var_dump($allpages);
It is late, and I am sleepy, but I think that duplicates your function with the exception that your code, unnecessarily to my mind, produces nested arrays. My code gives you a “flat” array.