If you need full sitemap, you should read all records (one SELECT
I assume) and organize in hierarchy in the PHP code.
Sample code:
// TODO: get this from DB
$list = array(
array('ID' => 1, 'post_title' => 'Skate brand no1', 'post_parent' => 5),
array('ID' => 2, 'post_title' => 'Skate brand no2', 'post_parent' => 5),
array('ID' => 3, 'post_title' => 'Products', 'post_parent' => 0),
array('ID' => 4, 'post_title' => 'Bikes', 'post_parent' => 3),
array('ID' => 5, 'post_title' => 'Skateboards', 'post_parent' => 3),
array('ID' => 6, 'post_title' => 'About', 'post_parent' => 0),
array('ID' => 7, 'post_title' => 'Contact', 'post_parent' => 6),
array('ID' => 8, 'post_title' => 'Surfboards', 'post_parent' => 3),
);
$rootPages = array();
$pagesById = array();
foreach ($list as $key => &$row) {
if ($row['post_parent'] == 0) {
$rootPages[] = &$row;
}
}
foreach ($list as $key => &$row) {
$pagesById[$row['ID']] = &$row;
}
foreach ($list as $key => &$row) {
if ($row['post_parent'] != 0) {
$pagesById[$row['post_parent']]['children'][] = &$row;
}
}
print_r($rootPages);