Created
June 6, 2017 18:22
-
-
Save tarecord/b022e32b0df0d6c93bbbdc3c6927e563 to your computer and use it in GitHub Desktop.
Get the top most parent of a WordPress post
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
/** | |
* Gets the top most ancestor for a post. | |
* | |
* @param object $post The current post | |
* | |
* @return integer The post ID of the ancestor | |
*/ | |
function get_top_ancestor( $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