Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active July 1, 2016 15:16
Show Gist options
  • Save morgyface/5ea8bfc9a0136509275f83a7858658c0 to your computer and use it in GitHub Desktop.
Save morgyface/5ea8bfc9a0136509275f83a7858658c0 to your computer and use it in GitHub Desktop.
WordPress | List bottom level childless posts
<?php
$args = array(
'post_type' => 'posts',
'posts_per_page' => 99,
'orderby' => 'parent',
'order' => 'ASC'
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
$childargs = array(
'post_type' => 'posts',
'child_of' => $post->ID
);
$children = get_pages( $childargs );
$childcount = count( $children );
if( $childcount == 0 ) {
$title = get_the_title( $post->ID );
echo '<li>' . $title . '</li>';
}
} // close foreach
?>
@morgyface
Copy link
Author

Not the older generation, just the young

Sometimes you want to ignore the top level posts and generate a list of only the bottom level posts, the childless.

This achieves that using a combination of get_posts and get_pages.

It essentially asks if the post has any children and if not it displays it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment