我创建了自定义帖子类型和用户角色。功能为新用户角色分配功能。当我转到edit.php?post_type = cv页面(使用admin用户)时,我看到其他用户cv帖子但我无法编辑它们。我已经指定了' edit_others_cvs'能力...所以我不明白为什么我不能编辑用户帖子。你能救我吗?
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');
答案 0 :(得分:1)
get_role()
函数返回一个WP_Role
对象,因此您需要比较该对象的name
属性而不是对象本身。否则,$role == 'administrator'
条件永远不会成立。
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' );
}
}
}