管理员无法编辑其他用户的自定义帖子类型 [英] the admin can't edit the other users custom post type
问题描述
我创建了一个自定义帖子类型和一个用户角色.函数将功能分配给新用户角色.当我进入edit.php?post_type = cv页面(使用admin用户)时,我看到其他用户的简历,但我无法对其进行编辑.我已经分配了'edit_others_cvs'功能...所以我不明白为什么我不能编辑用户帖子.你能帮我吗?
I have created a custom post type and a user role. A function assign capabilities to the new user role. When I go to the edit.php?post_type=cv page (with the admin user) I see the other users cv posts but I can't edit them. I have assign the 'edit_others_cvs' capabilitie...so I don't understand why I can't edit the users posts. Can you help me ?
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'
条件将永远不成立.
The get_role()
function returns a WP_Role
object, so what you need is to compare the name
property of that object instead of the object itself. Otherwise, the $role == 'administrator'
condition will never be true.
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' );
}
}
}
这篇关于管理员无法编辑其他用户的自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!