//first let's separate all the top level pages & child pages
$top_lvl = array();
$children = array();
foreach ( $values as $post ) {
if( $post->post_parent ) {
$children[$post->post_parent][] = $post;
} else {
$top_lvl[] = $post;
}
}
//now we have all top level pages in $top_lvl
//& we can access their child pages at $children[<top_lvl_ID>]
//now lets loop through top_lvl pages & display them
foreach ( $top_lvl as $post ) {
setup_postdata( $post );
$output .= '<li data-id="'. $post->ID .'" data-area="'. $area['area'] .'"><p>'. get_the_title() .'</p><span>'. __('Parent', 'blocks') .'</span></li>';
//check if this post has children
if( isset( $children[$post->ID] ) ) {
foreach( $children[$post->ID] as $post ) {
//do something with children here
}
}
}
//can't forget to reset the global data
wp_reset_postdata();