【发布时间】:2021-03-16 00:12:56
【问题描述】:
我被困在一个可能很简单的问题上。我有一个使用以下代码注册的 CPT 来定义特殊功能:
$args = [
"label" => "Tracts",
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_nav_menus" => true,
"map_meta_cap" => true,
"rewrite" => [ "slug" => "tract", "with_front" => true ],
"query_var" => true,
"supports" => [ "title", "editor", "thumbnail", "excerpt" ],
'capabilities' => array(
'edit_posts' => 'edit_tracts',
'edit_others_posts' => 'edit_others_tracts',
'delete_posts' => 'delete_tracts',
'publish_posts' => 'publish_tracts',
)
];
register_post_type( "tract", $args );
然后我正在注册一个新角色。此角色只需要能够查看和编辑此自定义帖子类型。我不能让他们看到常规帖子。
add_role('land_editor', 'Land Editor', array(
'read' => true,
'edit_tracts' => true,
'edit_others_tracts' => true,
'delete_tracts' => true,
'publish_tracts' => true,
));
使用此代码,我的角色可以完成我需要它执行的所有操作(他们可以查看 CPT、对其进行编辑等),但在自定义帖子类型中添加新帖子除外。每当我单击“添加新”时,我都会进入“抱歉,您无权访问此”页面。但是,当我将“edit_posts”添加到新角色的能力数组时,我可以突然添加自定义帖子类型的新帖子。但是,这也使用户能够查看和添加所有其他帖子类型的新内容,所以我不能这样做。关于为什么 edit_tracts 不允许我添加新 tracts 的任何见解?
【问题讨论】: