WordPress in_tree() function

January 11, 2010

Just created a wordpress funciton in_tree($a, $b) which checks to see if page ID $a is in the tree of page ID $b. That is, it checks to see if $a is a child of $b or $a is $b.

/**
 * Checks to see if a $in_tree_id is in the tree of
 * $in_tree_root post (is a child of or is the post
 * ID provided)
 */
function in_tree($in_tree_id, $in_tree_root)
{
	if($in_tree_id == $in_tree_root)
		return true;
	if(!get_post($in_tree_id)->post_parent)
		return false;
	return in_tree(get_post($in_tree_id)->post_parent, $in_tree_root);
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive