WordPress is_tree Functions for Custom Post Types & Ancestors

The is_tree function has been floating around for years, however it’s grown a bit outdated and doesn’t accommodate for custom post types, or for ancestors of a page. So right now the old is_tree function can tell if a Page is a child of another Page, but not if it’s a “grandchild” of a particular Page.

Here’s the update, which will do everything as promised:

function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
$cpid = get_the_ID();
$parents = get_post_ancestors( $post->ID ); // get the ancestors
$ancestorid = ($parents) ? $parents[count($parents)-1]: $post->ID;
return true; // we're at the page or at a sub page
return false; // we're elsewhere


Up Next: How to Transfer a WordPress Website