0
votes

In my wordpress site i created two custom post types: "quaderni" and "ricerche".

function my_custom_post_quaderni() {
    $labels = array(
        'name'               => _x( 'Quaderni', 'post type general name' ),
        'singular_name'      => _x( 'Quaderni', 'post type singular name' ),
        'add_new'            => _x( 'Aggiungi Nuovo', 'quaderno' ),
        'add_new_item'       => __( 'Aggiungi nuovo Quaderno' ),
        'edit_item'          => __( 'Modifica Quaderno' ),
        'new_item'           => __( 'Nuovo Quaderno' ),
        'all_items'          => __( 'Tutti i Quaderni' ),
        'view_item'          => __( 'Vedi Quaderno' ),
        'search_items'       => __( 'Cerca Quaderni' ),
        'not_found'          => __( 'Nessun Quaderno trovato' ),
        'not_found_in_trash' => __( 'Nessun Quaderno nel cestino' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Quaderni'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Database Quaderni',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor'),
        'has_archive'   => true,
    );
    register_post_type( 'quaderni', $args );
}
add_action( 'init', 'my_custom_post_quaderni' );

function my_custom_post_ricerche() {
    $labels = array(
        'name'               => _x( 'Ricerche', 'post type general name' ),
        'singular_name'      => _x( 'Ricerche', 'post type singular name' ),
        'add_new'            => _x( 'Aggiungi Nuovo', 'ricerca' ),
        'add_new_item'       => __( 'Aggiungi nuova ricerca' ),
        'edit_item'          => __( 'Modifica Ricerca' ),
        'new_item'           => __( 'Nuovo Ricerca' ),
        'all_items'          => __( 'Tutti le Ricerche' ),
        'view_item'          => __( 'Vedi Ricerca' ),
        'search_items'       => __( 'Cerca Ricerche' ),
        'not_found'          => __( 'Nessuna Ricerca trovata' ),
        'not_found_in_trash' => __( 'Nessuna Ricerca nel cestino' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Ricerche'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Database Ricerche',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor'),
        'has_archive'   => true,
    );
    register_post_type( 'ricerche', 
                        $args,
                        array(
                            'public' => true,
                            'capability_type' => 'ricerche',
                            'capabilities' => array(
                                'publish_posts' => 'publish_ricerche',
                                'edit_posts' => 'edit_ricerche',
                                'edit_others_posts' => 'edit_others_ricerche',
                                'read_private_posts' => 'read_private_ricerche',
                                'edit_post' => 'edit_ricerche',
                                'delete_post' => 'delete_ricerche',
                                'read_post' => 'read_ricerche',
                            ),
                        )
                        );
}
add_action( 'init', 'my_custom_post_ricerche' );

I created a "ricercatore2" custom user. He can edit and read post, and custom posts.

add_role('ricercatore2',
        'Ricercatore2',
        array(
            'read' => true,
            'edit_posts' => true,
            'delete_posts' => true,
            'publish_posts' => false,
            'upload_files' => true,
        )
    );
function add_capability()
        {
            $role = get_role('ricercatore2');
            $role->add_cap('read_ricerche');
            $role->add_cap('edit_ricerche');
            $role->add_cap('delete_ricerche', false); // to be sure
        }
        add_action('admin_init', 'add_capability');

I would like to make him be able only to edit the "ricerche" custom post type. But as now the user can edit also the "quaderni" custom post type and the regular post. Or plugin stuff like forms from contact form 7. Is it possible to limit the custom user to edit only one type of custom post in wordpress?

1

1 Answers

0
votes

look like you add cap for new role here

add_role('ricercatore2',
        'Ricercatore2',
        array(
            'read' => true,
            'edit_posts' => true,
            'read_posts' => true,
            'publish_posts' => false,
            'upload_files' => false,
        )
    );

why you not make

add_role('ricercatore2',
        'Ricercatore2',
        array(
            'read' => false,
            'edit_ricerche' => true,
            'delete_ricerche' => true,
            'publish_posts' => false,
            'upload_files' => false,
        )
    );

Also if you add some custom role and cap, and after make changes - try to delete role and cap