symdesign
7/21/2015 - 2:21 PM

Get Root ID

function get_the_root_ID() {
    global $post;
    
    $post_id = $post->ID; //or somehow get the current post ID.
    $ancestors = get_post_ancestors($post_id); 
    //$ancestors is an array of post IDs starting with the current post going up the root
    //'Pop' the root ancestor out or returns the current ID if the post has no ancestors.
    $root_id = (!empty($ancestors) ? array_pop($ancestors): $post_id);
    
    return $root_id;
}