[wp-hackers] get_post_ancestors() inside a custom function

Steve Taylor steve at sltaylor.co.uk
Wed Oct 21 14:17:36 UTC 2009


I'm trying to use get_post_ancestors() to implement some extra
permissions checking, basically to restrict editing and deleting of
particular "sections". To briefly explain the function given below:

- For testing, I'm hard-coded the ID of the page that is the parent of
this "section" of the site (it's 12)
- I'm using Justin Tadlock's new Members plugin for managing
capabilities, and 'manage_conference_deff' is a particular capability
I've added that is needed to manage pages in this section.

Viewing the Edit Pages list as a user without the right capability
hides the Edit and Delete links for the main parent page, but not for
any descendants. It seems that get_post_ancestors( $postID ) is
returning an empty array. I can't understand why it's not working -
$postID is definitely coming through OK from $args[0]. Is there any
reason that get_post_ancestors() wouldn't work inside a custom
function?

function slt_map_meta_cap( $caps, $cap, $user_id, $args ) {
	$postID = $args[0];
	switch ( $cap ) {
		case "edit_page":
		case "delete_page": {
			$ancestors = get_post_ancestors( $postID );
			if ( $postID == 12 || in_array( 12, $ancestors ) )
				$caps[] = 'manage_conference_deff';
			break;
		}
	}
	return $caps;
}
add_filter( 'map_meta_cap', 'slt_map_meta_cap', 10, 4 );

thanks,

Steve


More information about the wp-hackers mailing list