Skip to content

Instantly share code, notes, and snippets.

@aaronranard
Last active November 19, 2015 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronranard/75e7311a65425b0625f2 to your computer and use it in GitHub Desktop.
Save aaronranard/75e7311a65425b0625f2 to your computer and use it in GitHub Desktop.
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