0

I created a custom post type and gave it the following capabilities:

add_action( 'init', 'create_team_page_post_type' );

function create_team_page_post_type() {
$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_pages' => 'publish_teams',
            'edit_pages' => 'edit_teams',
            'edit_page' => 'edit_team',
            'edit_others_pages' => 'edit_others_teams',             
            'delete_pages' => 'delete_teams',
            'delete_page' => 'delete_team',
            'delete_others_pages' => 'delete_others_teams',
            'manage_pages' => 'manage_teams',
            'read_private_pages' => 'read_private_teams',               
            'read_page' => 'read_team'
        ),
    'map_meta_cap'=> true,      
    'has_archive' => true,
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title',
        'author',
        'editor',
        'thumbnail',
    )
);

register_post_type( 'team_page', $args );
}

I also checked $GLOBALS['wp_post_types']['team_page']to make sure they were all there and everything seems to be behaving as it should:

[cap] => stdClass Object
    (
        [edit_post] => edit_team
        [read_post] => read_team
        [delete_post] => delete_team
        [edit_posts] => edit_teams
        [edit_others_posts] => edit_others_teams
        [publish_posts] => publish_teams
        [read_private_posts] => read_private_teams
        [read] => read
        [delete_posts] => delete_teams
        [delete_private_posts] => delete_private_teams
        [delete_published_posts] => delete_published_teams
        [delete_others_posts] => delete_others_teams
        [edit_private_posts] => edit_private_teams
        [edit_published_posts] => edit_published_teams
        [create_posts] => edit_teams
    )

So I'm guessing my problem lies in incorrectly assigning these capabilities which I did like so:

$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');

I thought that was the problem but when I print_r(get_role( 'administrator' )); I get:

 WP_Role Object
(
[name] => administrator
[capabilities] => Array
    (
        [manage-teams] => 1
        [edit-teams] => 1
        [edit_others_teams] => 1
        [delete_teams] => 1
        [edit-team] => 1
        [delete_others_teams] => 1
        [delete_team] => 1
        [read_private_teams] => 1
        [read_team] => 1
        [manage_teams] => 1
    )
)

That list is shortened considerably.

Anyway it still doesn't work. I cannot access my custom post type as admin unless I change the capability type back to "page" or "post."

2 Answers 2

1

You are mixing hyphens and underscores in the cap names you are adding to the administrator role. These don't always correspond to the caps you created.

1
  • That helped hugely. It is now appearing on the dashboard. I can view teams but I can't edit or delete them. I tidied things up and checked that I spelled team correctly but I'm still missing something.
    – mantis
    Jul 3, 2013 at 7:48
0

There is a note in the Codex under capability_type that "It seems that map_meta_cap needs to be set to true, to make this work." Based purely on that note in the Codex I'd say you need to add...

'map_meta_cap' => true,

... to your argument list.

If that doesn't work please post all of the relevant code.

5
  • actually I already had that but I will post it with the code as t is pertinant.
    – mantis
    Jul 2, 2013 at 21:20
  • I added all the relevant code I could think of. Thanks for looking. I thought I'd find more examples of this.
    – mantis
    Jul 2, 2013 at 22:12
  • I will install and test that later, if no one beats me to an answer.
    – s_ha_dum
    Jul 2, 2013 at 22:13
  • unlikely, especially as I'm going to bed ;)
    – mantis
    Jul 2, 2013 at 22:16
  • It seems to be something with the edit_teams, edit_team, delete_teams, delete_team or perhaps I'm missing a capability?
    – mantis
    Jul 3, 2013 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.