Last active
July 1, 2016 15:16
-
-
Save morgyface/5ea8bfc9a0136509275f83a7858658c0 to your computer and use it in GitHub Desktop.
WordPress | List bottom level childless posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.