Last active
October 5, 2015 22:47
-
-
Save not-only-code/2889643 to your computer and use it in GitHub Desktop.
Wordpress: get hierarchical post ancestor
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
/** | |
* get hierarchical post ancestor (level 0) | |
* | |
* @param int|object $post | |
* @return object post ancestor | |
* | |
* @usage get_post_ancestor(131) | get_post_ancestor($post) | |
* | |
**/ | |
function get_post_ancestor( $post = false ) { | |
if ( !is_object($post) ) { | |
$post = get_post($post); | |
} | |
if ( !$post || empty($post->ancestors) ) { | |
return; | |
} | |
return get_post( end( $post->ancestors ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment