wordpress自定义帖子类型功能读取(cpt)不可用
我在functions.php文件中创建了一个自定义post类型“produktionsauftrag”。对于特定的用户角色(woocommerce的shop_manager),我需要此自定义帖子类型的特殊权限 商店经理应该能够创建帖子,但不能编辑(只是阅读)。当我使用自己的功能类型创建自定义帖子类型时,我可以更改权限,但没有read\u cpt选项,如您所见: 如何添加read_auftrags选项?functions.php中的自定义帖子类型:wordpress自定义帖子类型功能读取(cpt)不可用,wordpress,Wordpress,我在functions.php文件中创建了一个自定义post类型“produktionsauftrag”。对于特定的用户角色(woocommerce的shop_manager),我需要此自定义帖子类型的特殊权限 商店经理应该能够创建帖子,但不能编辑(只是阅读)。当我使用自己的功能类型创建自定义帖子类型时,我可以更改权限,但没有read\u cpt选项,如您所见: 如何添加read_auftrags选项?functions.php中的自定义帖子类型: function cptui_registe
function cptui_register_my_cpts_produktionsauftrag() {
/**
* Post Type: Produktionsaufträge.
*/
$labels = array(
"name" => __( "Produktionsaufträge", "" ),
"singular_name" => __( "Produktionsauftrag", "" ),
"menu_name" => __( "Produktionsaufträge", "" ),
"all_items" => __( "Alle Produktionsaufträge", "" ),
"add_new" => __( "Produktionsauftrag erstellen", "" ),
"add_new_item" => __( "Produktionsauftrag erstellen", "" ),
"edit_item" => __( "Produktionsauftrag anpassen", "" ),
"new_item" => __( "Neuer Produktionsauftrag", "" ),
"view_item" => __( "Produktionsauftrag anzeigen", "" ),
"view_items" => __( "Produktionsaufträge anzeigen", "" ),
"search_items" => __( "Produktionsauftrag suchen", "" ),
"not_found" => __( "Keine Produktionsaufträge gefunden", "" ),
"not_found_in_trash" => __( "Keine Produktionsaufträge gefunden", "" ),
"items_list" => __( "Produktionsauftragsliste", "" ),
);
$args = array(
"label" => __( "Produktionsaufträge", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => false,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
'capability_type' => 'auftrag',
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "produktionsauftrag", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "author" ),
);
register_post_type( "produktionsauftrag", $args );
}
add_action( 'init', 'cptui_register_my_cpts_produktionsauftrag' );
read
功能不是可以应用于CPT的标准功能,但看起来您可以设置它
作为能力阵列的一部分,CPT可接受的能力包括:
- 编辑帖子、编辑其他帖子、发布帖子、阅读私人帖子
- 以及元功能:编辑帖子、阅读帖子和删除帖子
- 阅读、编辑发布的帖子、编辑私人帖子、创建帖子、删除帖子、删除私人帖子、删除发布的帖子、删除其他帖子
“map\u meta\u cap”=>true时,可能会自动完成,但不清楚):
然后您应该将这些功能添加到相应的用户角色中。请注意,其中一个答案中提到,您需要向管理员添加功能,以便能够在管理员中编辑帖子
add_action( 'init', 'add_produktionsauftrag_caps_role');
function add_produktionsauftrag_caps_role() {
/* Get the roles you want to add capabilities for, e.g. */
$roles = array( get_role('shop_manager'), get_role('administrator') );
/* Add the capabilities for each role */
foreach($roles as $role) {
if($role) {
/* Add the primitive capabilities, e.g.: */
$role->add_cap( 'edit_auftrag' );
$role->add_cap( 'edit_auftrags' );
$role->add_cap( 'edit_others_auftrags' );
$role->add_cap( 'publish_auftrags' );
$role->add_cap( 'read_auftrag' );
$role->add_cap( 'read_private_auftrag' );
$role->add_cap( 'delete_auftrag' );
$role->add_cap( 'edit_published_auftrags' );
$role->add_cap( 'delete_published_auftrags' );
}
}
}
如果上述方法无效:
在wordpress.stackexchange.com上的一些问题中使用了上述内容,尽管有些问题没有在CPT中设置功能数组。。。他们只向角色添加功能(本答案中的第二段代码)。如果上述代码有问题,请尝试仅添加add\u produktionsauftrag\u caps\u角色
函数
即使这些问题/答案都是这样,法典委员会说,元能力不应分配给任何角色,它们必须映射到map\u meta\u cap()
中相应的基本能力
此外,根据法典:
当某个用户的角色只有post类型功能时,创建新对象是不够的。。。这是因为自定义帖子类型的元功能没有被自动映射,所以我们无法对权限进行粒度控制。要映射自定义帖子类型的元功能,我们可以使用map\u meta\u cap
hook,如下所述:
。。。因此,即使wordpress.stackexchange.com上的问题/答案没有使用映射元数据cap
,您也可能需要这样做
参考资料:
默认情况下,所有帖子都启用读取功能。你的文章阅读权限有问题吗?没有,我的文章阅读权限没有问题。问题是,我创建了一个自定义帖子类型,一个用户角色应该能够发布此帖子类型的新帖子并阅读它,但发布后不能编辑它。在插件用户角色编辑器中,我看不到read_(cpt名称)only read_private_(cpt名称)功能。这是因为“read”是所有帖子的默认设置,因此您没有能力为自定义帖子类型打开和关闭。所以我尝试做的是不可能的?我需要的是,我可以设置权限,以便用户可以创建帖子,但他不应该能够更改帖子。读取权限不会对您尝试执行的操作产生任何影响-他们不能使用仅读取权限更改任何内容。您需要更改的是他们的编辑权限。非常感谢您的帮助。我试图获得读取功能,但在您的帮助下无法获得。我仍然只是有一个read_private(postname)功能。我知道它是用javascript解决的,这不是一个好的解决方案,但它确实是我所拥有的一切。@Peesen87我不确定你甚至可以通过插件设置元功能(你的屏幕截图看起来像是使用用户角色编辑器?)。这个答案应该直接在代码中设置功能,而不需要使用单独的插件,这样读取功能就不会出现在插件中:)
add_action( 'init', 'add_produktionsauftrag_caps_role');
function add_produktionsauftrag_caps_role() {
/* Get the roles you want to add capabilities for, e.g. */
$roles = array( get_role('shop_manager'), get_role('administrator') );
/* Add the capabilities for each role */
foreach($roles as $role) {
if($role) {
/* Add the primitive capabilities, e.g.: */
$role->add_cap( 'edit_auftrag' );
$role->add_cap( 'edit_auftrags' );
$role->add_cap( 'edit_others_auftrags' );
$role->add_cap( 'publish_auftrags' );
$role->add_cap( 'read_auftrag' );
$role->add_cap( 'read_private_auftrag' );
$role->add_cap( 'delete_auftrag' );
$role->add_cap( 'edit_published_auftrags' );
$role->add_cap( 'delete_published_auftrags' );
}
}
}