Using WordPress ‘get_post_type_capabilities()’ PHP function

The get_post_type_capabilities() WordPress PHP function builds an object with all post type capabilities out of a post type object.

Usage

$capabilities = get_post_type_capabilities( $post_type_args );

Parameters

  • $post_type_args (object) – Required. Post type registration arguments.

More information

See WordPress Developer Resources: get_post_type_capabilities()

Examples

Register a custom post type with specific capabilities

This code registers a custom post type called “book” with a custom set of capabilities.

function create_book_post_type() {
    $args = array(
        'label' => 'Books',
        'public' => true,
        'capability_type' => 'book',
        'capabilities' => array(
            'edit_post' => 'edit_book',
            'read_post' => 'read_book',
            'delete_post' => 'delete_book',
            'edit_posts' => 'edit_books',
            'edit_others_posts' => 'edit_others_books',
            'delete_posts' => 'delete_books',
            'publish_posts' => 'publish_books',
            'read_private_posts' => 'read_private_books',
        ),
    );
    register_post_type('book', $args);
}
add_action('init', 'create_book_post_type');

Modify capabilities of an existing post type

This code modifies the capabilities of the built-in “post” post type, so that only users with the “manage_options” capability can edit or delete posts.

function modify_post_type_capabilities() {
    global $wp_post_types;

    $capabilities = array(
        'edit_post' => 'manage_options',
        'delete_post' => 'manage_options',
        'edit_posts' => 'manage_options',
        'delete_posts' => 'manage_options',
    );

    $wp_post_types['post']->cap = (object) array_merge((array) $wp_post_types['post']->cap, $capabilities);
}
add_action('init', 'modify_post_type_capabilities', 20);

Check if a user can edit a custom post type

This code checks if the current user can edit a “book” post type with the ID $book_id.

function can_user_edit_book($book_id) {
    return current_user_can('edit_book', $book_id);
}

Grant custom capabilities to a user role

This code grants the custom “book” capabilities to the “editor” role.

function grant_book_capabilities_to_editor() {
    $role = get_role('editor');

    $capabilities = array(
        'edit_book', 'read_book', 'delete_book', 'edit_books',
        'edit_others_books', 'delete_books', 'publish_books',
        'read_private_books',
    );

    foreach ($capabilities as $cap) {
        $role->add_cap($cap);
    }
}
add_action('init', 'grant_book_capabilities_to_editor');

Remove custom capabilities from a user role

This code removes the custom “book” capabilities from the “editor” role.

function remove_book_capabilities_from_editor() {
    $role = get_role('editor');

    $capabilities = array(
        'edit_book', 'read_book', 'delete_book', 'edit_books',
        'edit_others_books', 'delete_books', 'publish_books',
        'read_private_books',
    );

    foreach ($capabilities as $cap) {
        $role->remove_cap($cap);
    }
}
add_action('init', 'remove_book