Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: WordPress return top most parent ID
<?php
/**
* [Gets the top most parent of a post. If post is top most parent, returns ID]
* @return int [ID of top most parent]
*/
function get_earliest_relative($post){
if ($post->post_parent){
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
return $parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.