Skip to content

Instantly share code, notes, and snippets.

@not-only-code
Last active October 5, 2015 22:47
Show Gist options
  • Save not-only-code/2889643 to your computer and use it in GitHub Desktop.
Save not-only-code/2889643 to your computer and use it in GitHub Desktop.
Wordpress: get hierarchical post ancestor
/**
* 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