map_meta_cap woes

I have created a custom post type and want it to behave like a page. Certain roles need to have access to it and not to posts or pages. So I created my custom post type with a capability-type and capabilities like so:

function add_team_page_post_type($name, $args= array()) {
add_action('init', function() use($name, $args) {
        $args = array(
            'labels' => team_page_post_type_labels( 'Team Page' ),
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => array('team', 'teams'),
            'capabilities' => array(
                'publish_posts' => 'publish_teams',
                'edit_posts' => 'edit_teams',
                'edit_post' => 'edit_team',
                'edit_others_posts' => 'edit_others_teams',
                'delete_posts' => 'delete_teams',
                'delete_post' => 'delete_team',
                'delete_others_posts' => 'delete_others_teams',
                'manage_posts' => 'manage_teams',
                'read_private_posts' => 'read_private_teams',
                'read_post' => 'read_team',
            ),
            'map_meta_cap'=> true,  
            'has_archive' => true,
            'hierarchical' => true,
            'menu_position' => null,
            'supports' => array('title',
                'author',
                'editor',
                'thumbnail',
            ));

        register_post_type( $name, $args );
    });
}

add_team_page_post_type('team_page', array());

And am assigning them to various roles like so:

Read More
add_action( 'init', 'my_custom_capabilities', 0);
function my_custom_capabilities(){

$role_object = get_role( 'administrator' );
    $role_object->add_cap( 'publish_teams');
    $role_object->add_cap( 'edit_teams');
    $role_object->add_cap( 'edit_team');
    $role_object->add_cap( 'edit_others_teams');
    $role_object->add_cap( 'delete_teams');
    $role_object->add_cap( 'delete_team');
    $role_object->add_cap( 'delete_others_teams');
    $role_object->add_cap( 'manage_teams');        
    $role_object->add_cap( 'read_private_teams');
    $role_object->add_cap( 'read_team');
}

Upon further research I’ve found I needed to include a map_meta_cap()function which I did like so :

add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );

function my_map_meta_cap( $caps, $cap, $user_id, $args ) {

/* If editing, deleting, or reading a team, get the post and post type object. */
if ( 'edit_team' == $cap || 'delete_team' == $cap || 'read_team' == $cap ) {
    $post = get_post( $args[0] );
    $post_type = get_post_type_object( $post->post_type );

    /* Set an empty array for the caps. */
    $caps = array();
}

/* If editing a team, assign the required capability. */
if ( 'edit_team' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->edit_posts;
    else
        $caps[] = $post_type->cap->edit_others_posts;
}

/* If deleting a team, assign the required capability. */
elseif ( 'delete_team' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->delete_posts;
    else
        $caps[] = $post_type->cap->delete_others_posts;
}

/* If reading a private team, assign the required capability. */
elseif ( 'read_team' == $cap ) {

    if ( 'private' != $post->post_status )
        $caps[] = 'read';
    elseif ( $user_id == $post->post_author )
        $caps[] = 'read';
    else
        $caps[] = $post_type->cap->read_private_posts;
}

/* Return the capabilities required by the user. */
return $caps;
}

This comes from an article by Justin Tadlock http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types

The problem is that I can only view my cpt and not edit or delete it. Am I guessing the problem is with the last function but I can’t find much documentation on it.

Related posts

1 comment

Comments are closed.