Search code examples
wordpresscustom-post-typeuser-roles

the admin can't edit the other users custom post type


I have created a custom post type and a user role. A function assign capabilities to the new user role. When I go to the edit.php?post_type=cv page (with the admin user) I see the other users cv posts but I can't edit them. I have assign the 'edit_others_cvs' capabilitie...so I don't understand why I can't edit the users posts. Can you help me ?

function cv_custom_posttype() {

$labels = array(
    'not_found'          => 'cv not found'
);

$args = array(
    'labels'                => $labels,
    'public'                => true,
    'publicly_queryable'    => true,
    'query_var'             => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'hierarchical'          => false,
    'menu_position'         => 5,
    'register_meta_box_cb'  => 'add_cv_metaboxes', 
    'supports'              => false, 
    'has_archive'           => false,
    'capability_type'       => array("cv", "cvs"),
    'map_meta_cap'          => true
);
register_post_type( 'cv', $args );

function add_cv_user_role() {
 add_role('cv_member',
            'cv member',
            array(
                'read' => true,
                'edit_posts' => false,
                'delete_posts' => false,
                'publish_posts' => false
            )
        );
 }
register_activation_hook( __FILE__, 'add_cv_user_role' );

function cv_add_role_caps() {

        // Add the roles you'd like to administer the custom post types
        $roles = array('administrator', 'cv_member');

        // Loop through each role and assign capabilities
        foreach($roles as $the_role) { 

             $role = get_role($the_role);

                 $role->add_cap( 'read' );
                 $role->add_cap( 'read_cv');
                 $role->add_cap( 'edit_cv' );
                 $role->add_cap( 'edit_cvs' );
                 $role->add_cap( 'edit_published_cvs' );
                 $role->add_cap( 'publish_cvs' );
                 $role->add_cap( 'delete_published_cvs' );

                 if($role == 'administrator') {
                    $role->add_cap( 'read_private_cvs' ); 
                    $role->add_cap( 'edit_others_cvs' );
                    $role->add_cap( 'delete_others_cvs' );
                    $role->add_cap( 'delete_private_cvs' );
                 }

        }
    }
add_action('admin_init','cv_add_role_caps');

Solution

  • The get_role() function returns a WP_Role object, so what you need is to compare the name property of that object instead of the object itself. Otherwise, the $role == 'administrator' condition will never be true.

    function cv_add_role_caps() {
        // Add the roles you'd like to administer the custom post types.
        $roles = array( 'administrator', 'cv_member' );
    
        // Loop through each role and assign capabilities.
        foreach ( $roles as $the_role ) {
            $role = get_role( $the_role );
    
            $role->add_cap( 'read' );
            $role->add_cap( 'read_cv' );
            $role->add_cap( 'edit_cv' );
            $role->add_cap( 'edit_cvs' );
            $role->add_cap( 'edit_published_cvs' );
            $role->add_cap( 'publish_cvs' );
            $role->add_cap( 'delete_published_cvs' );
    
            if ( 'administrator' === $role->name ) { // Accessing to the object's 'name' property.
                $role->add_cap( 'read_private_cvs' );
                $role->add_cap( 'edit_others_cvs' );
                $role->add_cap( 'delete_others_cvs' );
                $role->add_cap( 'delete_private_cvs' );
            }
        }
    }