get_post_ancestors() inside a custom function

View: New views
3 Messages — Rating Filter:   Alert me  

get_post_ancestors() inside a custom function

by Steve Taylor-15 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post_ancestors() inside a custom function

by scribu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> Is there any reason that get_post_ancestors() wouldn't work inside a custom
> function?
>

No, there's no reason that I can think of.

Try the code directly, i.e. in functions.php simply write:

var_dump(get_post_ancestors($some_id));

If it's working there, you should probably contact the plugin author, as
it's not an issue with WP Core.


--
http://scribu.net
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post_ancestors() inside a custom function

by Steve Taylor-15 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, scribu. Yes, the simple use of the get_post_ancestors()
outside any functions (in my theme's functions.php) works fine.

I'm out of look contacting the plugin author - it's me! I mentioned
Justin Tadlock's Members plugin as background to explain the
unorthodox capability referenced in my code.

FYI I'm basing my plugin on this:
http://pressography.com/plugin_template.txt  Seems generally solid.
The function I posted is a method of the plugin class.

Steve
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers