Last active
November 19, 2015 19:24
-
-
Save aaronranard/75e7311a65425b0625f2 to your computer and use it in GitHub Desktop.
PHP: WordPress return top most parent ID
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
<?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