• I have created custom post type

    add_action( 'init', 'oglasi_custom_post_type', 0 );
    // Creating a Deals Custom Post Type
    function oglasi_custom_post_type() {
      $labels = array(
        'name'                => __( 'Oglasi' ),
        'singular_name'       => __( 'Oglas'),
        'menu_name'           => __( 'Oglasi'),
        'parent_item_colon'   => __( 'Roditelj Oglas'),
        'all_items'           => __( 'Svi Oglasi'),
        'view_item'           => __( 'Pregledaj Oglas'),
        'add_new_item'        => __( 'Dodaj Novi Oglas'),
        'add_new'             => __( 'Dodaj Nov'),
        'edit_item'           => __( 'Izmeni Oglas'),
        'update_item'         => __( 'Ažuriraj Oglas'),
        'search_items'        => __( 'Traži Oglas'),
        'not_found'           => __( 'Nije Pronađen'),
        'not_found_in_trash'  => __( 'Nije Pronađen U Obrisanim')
      );
      $args = array(
        'label'               => __( 'oglasi'),
        'description'         => __( 'Vaši Oglasi'),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
        'public'              => true,
        'hierarchical'        => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => true,
        'can_export'          => true,
        'exclude_from_search' => false,
        'yarpp_support'       => true, 
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'capabilities' => array(
            'edit_post' => 'edit_oglas',
            'edit_posts' => 'edit_oglase',
            'edit_others_posts' => 'edit_other_oglase',
            'publish_posts' => 'publish_oglas',
            'read_post' => 'read_oglas',
            'read_private_posts' => 'read_private_oglase',
            'delete_post' => 'delete_oglas'
        ),
        'map_meta_cap' => false
    );
      register_post_type( 'oglasi', $args );
    }

    After that I have created a new custom user role so it can post only on CPT but now both new role and administrator can not see the custom post type.
    How do I make them see the custom post type?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi.
    Im not fully understand capability stuff, but according to the codex, args would be like this?

    'capability_type' => array( 'oglas', 'oglase' ),
    'map_meta_cap' => true

    It saids capabilities array will be constructed by capability_type arg.

    https://codex.wordpress.org/Function_Reference/register_post_type

    Thread Starter labyrinthman

    (@labyrinthman)

    When I create a CPT and want to assign capabilities to a custom user role I get this error.

    Fatal error: Uncaught Error: Call to a member function add_cap()...

    • This reply was modified 4 years, 4 months ago by labyrinthman.

    The main problem is that you mix two different approaches to create capabilities for your post type, you pass capability_type and capabilities in args array to register_post_type. Passing string as capability_type will cause creation of capabilities based on string or array of strings (singular and plural). For example if you pass
    capability_type' => array('oglas', 'oglase')
    then capabilities edit_oglas, edit_oglase and so on will be created automatically. And in this case you should not pass capabilities.
    So code for your post type should look like this:

    add_action( 'init', 'oglasi_custom_post_type', 0 );
    // Creating a Deals Custom Post Type
    function oglasi_custom_post_type() {
          $labels = array(
            'name'                => __( 'Oglasi' ),
            'singular_name'       => __( 'Oglas'),
            'menu_name'           => __( 'Oglasi'),
            'parent_item_colon'   => __( 'Roditelj Oglas'),
            'all_items'           => __( 'Svi Oglasi'),
            'view_item'           => __( 'Pregledaj Oglas'),
            'add_new_item'        => __( 'Dodaj Novi Oglas'),
            'add_new'             => __( 'Dodaj Nov'),
            'edit_item'           => __( 'Izmeni Oglas'),
            'update_item'         => __( 'Ažuriraj Oglas'),
            'search_items'        => __( 'Traži Oglas'),
            'not_found'           => __( 'Nije Pronađen'),
            'not_found_in_trash'  => __( 'Nije Pronađen U Obrisanim')
          );
          $args = array(
            'label'               => __( 'oglasi'),
            'description'         => __( 'Vaši Oglasi'),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
            'public'              => true,
            'hierarchical'        => false,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'has_archive'         => true,
            'can_export'          => true,
            'exclude_from_search' => false,
            'yarpp_support'       => true, 
            'publicly_queryable'  => true,
            'capability_type'     => array('oglas', 'oglase'),
        );
        
        register_post_type( 'oglasi', $args );
        
        
    }

    Now you should assign capabilities to admin, create custom role and assign capabilities to it too.

    function add_oglasi_caps() {
    
        $role = get_role( 'administrator' );
    
        $role->add_cap( 'edit_oglas' ); 
        $role->add_cap( 'read_oglas' ); 
        $role->add_cap( 'delete_oglas' ); 
        $role->add_cap( 'edit_oglase' ); 
        $role->add_cap( 'edit_others_oglase' ); 
        $role->add_cap( 'publish_oglase' ); 
        $role->add_cap( 'read_private_oglase' ); 
        $role->add_cap( 'edit_oglase' ); 
        
        
        add_role( 'oglasi_role', 'Oglasi role', array('read')); 
    
        $role = get_role( 'oglasi_role' );
        $role->add_cap( 'edit_oglas' ); 
        $role->add_cap( 'read_oglas' ); 
        $role->add_cap( 'delete_oglas' ); 
        $role->add_cap( 'edit_oglase' ); 
        $role->add_cap( 'edit_others_oglase' ); 
        $role->add_cap( 'publish_oglase' ); 
        $role->add_cap( 'read_private_oglase' ); 
        $role->add_cap( 'edit_oglase' ); 
        
    }
    add_action( 'admin_init', 'add_oglasi_caps');
    

    Remember that the code for custom role creation and adding capabilities should be executed once.

    • This reply was modified 4 years, 4 months ago by Jacek.
    Thread Starter labyrinthman

    (@labyrinthman)

    Thanks for the reply and the code @jzbydev but that did not help.
    Now the user with role oglasi_role can not go into the dashboard.

    I have copy-pasted your code and unfortunately, it’s not working.

    It looks like ogles_role does not have “read” capability, as is is required to access dashboard. Please check it by placing this code i functions.php or plugin:

    
    global $wp_roles;
    print_r($wp_roles->roles);
    

    You should see:

    
    ...
    [oglasi_role] => Array
            (
                [name] => Oglasi role
                [capabilities] => Array
                    (
                        ...
                        [read] => 1
                        ...
                    )
    
            )
    ...

    Can you see it?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Administrator lost it’s role on CPT’ is closed to new replies.