管理员无法编辑其他用户自定义帖子类型
问题描述:
我已经创建了自定义帖子类型和用户角色。功能为新用户角色分配功能。当我转到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');
答
的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');
}
}
}