Skip to content

Instantly share code, notes, and snippets.

@cipriantepes
Last active January 9, 2016 01:18
Show Gist options
  • Save cipriantepes/6a6084611b48ae17bee7 to your computer and use it in GitHub Desktop.
Save cipriantepes/6a6084611b48ae17bee7 to your computer and use it in GitHub Desktop.
WordPress loop through posts with hierarchy
<?php
private function get_all_posts( $post_id, $type = 'page' ) {
//* $posts is an array
$posts = get_posts( array(
'post_type' => $type,
'post_parent' => $post_id,
'nopaging' => true,
'orderby' => 'menu_order',
'hierarchical' => true,
'suppress_filters' => false
) );
foreach ( $posts as $key => $post ) {
$posts[$key] = (array) $post;
$posts[$key]['subnav'] = $this->get_all_posts( $post->ID );
}
return $posts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment