0

I asked about this a while ago https://stackoverflow.com/questions/66231525/wordpress-custom-user-roles-edit-others-posts

I changed my approach but it still doesn't work.

I've been attempting to create custom user roles that can only edit particular post types.

My reference: Restrict custom post type view by user role

Per the attached answer, I am passing in capability type and capabilities. the registering of the post type looks like below:

<?php

function engineering() {

    $labels = array(
        'name'                  => _x( 'Engineering Blog Posts', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Engineering Blog Posts', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Engineering Blog Posts', 'text_domain' ),
        'name_admin_bar'        => __( 'Engineering Blog Posts', 'text_domain' ),
        'archives'              => __( 'Engineering Blog Archives', 'text_domain' ),
        'attributes'            => __( 'Engineering Blog Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Engineering Blog Item:', 'text_domain' ),
        'all_items'             => __( 'All Engineering Blog Posts', 'text_domain' ),
        'add_new_item'          => __( 'Add New Engineering Blog Post', 'text_domain' ),
        'add_new'               => __( 'Add New Engineering Blog Post', 'text_domain' ),
        'new_item'              => __( 'New Engineering Blog Post', 'text_domain' ),
        'edit_item'             => __( 'Edit Engineering Blog Post', 'text_domain' ),
        'update_item'           => __( 'Update Engineering Blog Post', 'text_domain' ),
        'view_item'             => __( 'View Engineering Blog Post', 'text_domain' ),
        'view_items'            => __( 'View Engineering Posts', 'text_domain' ),
        'search_items'          => __( 'Search Engineering Blog Posts', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Engineering Blog', 'text_domain' ),
        'description'           => __( 'Engineering Blog', 'text_domain' ),
        'labels'                => $labels,
        'show_in_rest'          => true,
        'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
        'taxonomies'            => array( 'engineering_taxonomy' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-media-code',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'engineering',
        'capabilities' => array(
            'publish_posts' => 'publish_engineerings',
            'edit_posts' => 'edit_engineerings',
            'edit_others_posts' => 'edit_others_engineerings',
            'read_private_posts' => 'read_private_engineerings',
            'edit_post' => 'edit_engineering',
            'delete_post' => 'delete_engineering',
            'read_post' => 'read_engineering',
        ),
        'rewrite' => array('slug' => 'engineering-blog/post', 'with_front' => false)
    );
    register_post_type( 'engineering', $args );

}
add_action( 'init', 'engineering', 0 );


// Register Custom Taxonomy
function engineering_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Categories', 'text_domain' ),
        'all_items'                  => __( 'All Categories', 'text_domain' ),
        'parent_item'                => __( 'Parent Category', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Category:', 'text_domain' ),
        'new_item_name'              => __( 'New Category Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Category', 'text_domain' ),
        'edit_item'                  => __( 'Edit Category', 'text_domain' ),
        'update_item'                => __( 'Update Category', 'text_domain' ),
        'view_item'                  => __( 'View Category', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate categories with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove categories', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular categories', 'text_domain' ),
        'search_items'               => __( 'Search Categories', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No categories', 'text_domain' ),
        'items_list'                 => __( 'Categories list', 'text_domain' ),
        'items_list_navigation'      => __( 'Categories list navigation', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true,
        'rewrite'                    => array(
                                        'with_front' => false,
                                        'slug' => 'engineering-blog-category',
                                    ),
    );
    register_taxonomy( 'engineering_taxonomy', array( 'engineering' ), $args );

}
add_action( 'init', 'engineering_taxonomy', 0 );

in functions.php I am creating the user role and assigning the custom role and capabilities. That looks like this:

function userEngineering() {
    add_role('engineer', 'Engineer', array(
        'read' => true,
        'read_post' => true,
        'edit_posts' => true,
        'delete_posts' => true,
    ));
    $role = get_role('contributor');
    $role->add_cap('read_engineering');
    $role->add_cap('edit_engineering');
    $role->add_cap('delete_engineering');
}
add_action('admin_init', 'userEngineering');

When I am in my dashboard as an admin I can see the post type:

enter image description here

When I am in the account with the custom user role of "engineering", it disappears:

enter image description here

Why? Howcome the post type disappears?

I have it registered and the capabilities assigned.

Capability to edit own posts and not others

I have this capability (edit_others_posts)

What am I doing wrong?

Your Answer

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

Browse other questions tagged or ask your own question.