Created
May 18, 2016 06:25
-
-
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)
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
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