0

I've made some custom post types, and some user roles. Each role has access to different custom post types.

The thing is that edit_others_customposttypecapability, isn't working. I've tried logging in with the admin account, and it can't edit other's published custom post types.

I've installed User Role Editor plugin, checked each user's capabilities, and admin should be able to edit all site's posts.

Here are the user roles...

<?php 

/*

    This page loads all user roles that are necesary to the correct administration of the site.

*/

add_action('init', 'create_metaroles');

    function create_metaroles() {
        // Defino los roles
        $roles = array();
        $roles['Webmaster'] = array(
            'role'              =>  'webmaster',
            'caps'              =>  array(          
                array('pb_institucional','pb_institucionales'),
                array('pb_experto','pb_expertos'),
                array('pb_comite','pb_comites'),
                array('pb_reunion','pb_reuniones'),
                array('pb_progproy','pb_progproys'),
                array('pb_vacante','pb_vacantes'),
                array('pb_pais','pb_paises'),
                array('pb_organizacion','pb_organizaciones'),
                array('pb_evento','pb_eventos'),
                array('pb_noticia','pb_noticias'),
                array('pb_publicacion','pb_publicaciones'),
                array('pb_sector','pb_sectores'),
                array('pb_producto','pb_productos'),
                array('pb_conocimiento','pb_conocimientos')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor Institucional'] = array(
            'role'              =>  'gestor_institucional',
            'caps'              =>  array(          
                array('pb_institucional','pb_institucionales'),
                array('pb_experto','pb_expertos'),
                array('pb_comite','pb_comites'),
                array('pb_reunion','pb_reuniones')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Programas y Proyectos'] = array(
            'role'              =>  'gestor_progproy',
            'caps'              =>  array(
            array('pb_progproy','pb_progproys')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de RRHH'] = array(
            'role'              =>  'gestor_rrhh',
            'caps'              =>  array(
                array('pb_vacante','pb_vacantes')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Comunicacion Institucional'] = array(
            'role'              =>  'gestor_comunicacion',
            'caps'              =>  array(
                array('pb_pais','pb_paises'),
                array('pb_organizacion','pb_organizaciones'),
                array('pb_evento','pb_eventos'),
                array('pb_noticia','pb_noticias'),
                array('pb_publicacion','pb_publicaciones')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Sectores'] = array(
            'role'              =>  'gestor_sectores',
            'caps'              =>  array(
                array('pb_sector','pb_sectores')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Sectores, Programas y Proyectos'] = array(
            'role'              =>  'gestor_sectores',
            'caps'              =>  array(
                array('pb_sector','pb_sectores'),
                array('pb_sector','pb_sectores')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Productos'] = array(
            'role'              =>  'gestor_productos',
            'caps'              =>  array(
                array('pb_producto','pb_productos')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );

        $roles['Gestor de Biblioteca/Conocimiento'] = array(
            'role'              =>  'gestor_conocimiento',
            'caps'              =>  array(
                array('pb_conocimiento','pb_conocimientos')
            ),
            'capabilities'      =>  array(
                'read'          =>  true,
                'edit_posts'    =>  true,
                'delete_posts'  =>  true,
                'publish_posts' =>  true,
                'upload_files'  =>  true
            )
        );


        foreach( $roles as $name => $arr ){
            add_role($arr['role'], $name,
            array(
                'read'          =>  true,
                'edit_posts'    =>  false,
                'delete_posts'  =>  false,
                'publish_posts' =>  false,
                'upload_files'  =>  true
            ));

            $rol = get_role( $arr['role'] );

            foreach( $arr['caps'] as $cap ){
                $rol->add_cap( 'read' );
                $rol->add_cap( 'read_' . $cap[0] );
                $rol->add_cap( 'read_private_' . $cap[1] );
                $rol->add_cap( 'edit_' . $cap[1] );
                $rol->add_cap( 'edit_others_' . $cap[1] );
                $rol->add_cap( 'edit_published_' . $cap[1] );
                $rol->add_cap( 'publish_' . $cap[1] );
                $rol->add_cap( 'delete_' . $cap[0] );
                $rol->add_cap( 'delete_others_' . $cap[1] );
                $rol->add_cap( 'delete_private_' . $cap[1] );
                $rol->add_cap( 'delete_published_' . $cap[1] );
            }

            $rol = get_role('webmaster');

            $rol->add_cap( 'add_users' );
            $rol->add_cap( 'create_users' );
            $rol->add_cap( 'delete_users' );
            $rol->add_cap( 'edit_users' );
            $rol->add_cap( 'list_users' );
            $rol->add_cap( 'promote_users' );
            $rol->add_cap( 'remove_users' );
            $rol->add_cap( 'manage_categories' );
            $rol->add_cap( 'edit_plugins' );
            $rol->add_cap( 'edit_theme_options' );
            $rol->add_cap( 'manage_links' );
            $rol->add_cap( 'manage_options' );

            $rol = get_role( 'administrator' );

            foreach( $arr['caps'] as $cap ){
                $rol->add_cap( 'read' );
                $rol->add_cap( 'read_' . $cap[0] );
                $rol->add_cap( 'read_private_' . $cap[1] );
                $rol->add_cap( 'edit_' . $cap[1] );
                $rol->add_cap( 'edit_others_' . $cap[1] );
                $rol->add_cap( 'edit_others_' . $cap[0] );
                $rol->add_cap( 'edit_published_' . $cap[1] );
                $rol->add_cap( 'publish_' . $cap[1] );
                $rol->add_cap( 'delete_' . $cap[0] );
                $rol->add_cap( 'delete_others_' . $cap[1] );
                $rol->add_cap( 'delete_private_' . $cap[1] );
                $rol->add_cap( 'delete_published_' . $cap[1] );
            }
        }
}
?>

And i'll just copy one custom post type declaration (I'm using 15, but makes no sense copying all of'em)

class PB_Noticias {
        public function __construct(){
            $this->register_post_type();
        }

        public function register_post_type(){
            $labels = array(
                'name'                => __( 'Noticias', 'text-domain' ),
                'singular_name'       => __( 'Noticia', 'text-domain' ),
                'add_new'             => _x( 'Agregar Noticia', 'text-domain', 'text-domain' ),
                'add_new_item'        => __( 'Agregar Noticia', 'text-domain' ),
                'edit_item'           => __( 'Editar Noticia', 'text-domain' ),
                'new_item'            => __( 'Nuevo Noticia', 'text-domain' ),
                'view_item'           => __( 'Ver Noticia', 'text-domain' ),
                'search_items'        => __( 'Buscar Noticias', 'text-domain' ),
                'not_found'           => __( 'No se encontraron Noticias', 'text-domain' ),
                'not_found_in_trash'  => __( 'No se encontraron Noticias en la papelera', 'text-domain' ),
                'parent_item_colon'   => __( 'Noticia Padre:', 'text-domain' ),
                'menu_name'           => __( 'Noticias', 'text-domain' ),
            );

            $args = array(
                'labels'                   => $labels,
                'hierarchical'        => false,
                'description'         => 'Noticias OLADE',
                'taxonomies'          => array('category', 'post_tag'),
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 30,
                'menu_icon'           => admin_url() . 'images/project.png',
                'show_in_nav_menus'   => true,
                'publicly_queryable'  => true,
                'exclude_from_search' => false,
                'has_archive'         => true,
                'query_var'           => 'noticias',
                'can_export'          => true,
                'rewrite'             => array(
                    'slug' => 'noticias'
                    ),
                'capability_type'     => array('pb_noticia','pb_noticias'),
                'supports'            => array(
                    'title', 'editor'
                    )
            );
            register_post_type('pb_noticia', $args );
        }

        // Galeria
        // Foto grande
        // Cuerpo de la nota
    }

Any ideas why edit_others_cpt isn't working?

Thanks in advance!

1

Found it! I was missing one custom capability. To the code above, I added this line:

$rol->add_cap( 'edit_' . $cap[0] );

It gives the capability edit_ctpsinglename. (edit_pb_publicacion). And now it works just fine. Hope someone find's this useful.

0

Is your problem occurring in a multisite setup? If so, here is a solution:

http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/

By default in a multisite setup, only Super Administrators can edit users.

  • 1
    An answer should be more than just a link to an external site. Please add a solution. – fuxia Mar 22 '15 at 22:03
  • No. It's a custom made theme. Users are defined as detailed above. The thing is that I'm giving each post's custom capabilities to all administrators, but can't edit any custom post type, just can create new ones. Any ideas? – Pablo Mar 23 '15 at 21:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.