Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonathan-dejong/62ae6b4c18e935acc7be9788e6075dae to your computer and use it in GitHub Desktop.
Save jonathan-dejong/62ae6b4c18e935acc7be9788e6075dae to your computer and use it in GitHub Desktop.
A simple helper function for WordPress to check wether a post has grandchildren. Takes 1 parameter of a post id. Returns boolean (true or false)
function has_grandchildren( $post_id ){
$children = get_children(array(
'post_parent' => $post_id
));
if( $children ){
foreach( $children as $child ){
$grandchildren = get_children(array(
'post_parent' => $child->ID
));
if( $grandchildren ){
return true;
}
}
}
return false;
}
// Example usage
if( has_grandchildren( $post_id ) ){
//We have grandchildren!
}else{
//Sorry no grandchildre..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment