WordPress自定义POST类型功能问题

时间:2019-10-24 作者:Themesfa

使用WP 5.2.4,I am registering my custom post type with this code:

$args = array(
    \'labels\'          => $labels,
    \'public\'          => false,
    \'show_ui\'         => true,
    \'show_in_menu\'    => true,
    \'query_var\'       => true,
    \'has_archive\'     => false,
    \'menu_position\'   => null,
    \'map_meta_cap\'    => false,
    \'capability_type\' => [\'note\',\'notes\'],
    \'rewrite\'         => [ \'slug\' => \'note\', \'with_front\' => false ],
    \'supports\'        => [ \'editor\' ],
    \'menu_icon\'       => \'dashicons-format-aside\',
);

register_post_type( \'note\', $args );
Now I want my custom user role to edit/read/delete this post type. I added this caps to my custom user role:

$role = get_role( \'my_custom_role\' );

$role->add_cap(\'read_private_notes\');
$role->add_cap(\'read_note\');
$role->add_cap(\'read\');
$role->add_cap(\'publish_notes\');
$role->add_cap(\'edit_note\');
$role->add_cap(\'edit_notes\');
$role->add_cap(\'edit_others_notes\');
$role->add_cap(\'edit_private_notes\');
$role->add_cap(\'edit_published_notes\');
$role->add_cap(\'edit_notes\');
$role->add_cap(\'delete_note\');
$role->add_cap(\'delete_notes\');
$role->add_cap(\'delete_private_notes\');
$role->add_cap(\'delete_published_notes\');
$role->add_cap(\'delete_others_notes\');

Everything is OK for the administrator role and post type item shows in the admin menu. For other roles, it shows the admin menu item but in the post type list page give permission error.

cps show in admin menu

but cpt posts list not showin

如何解决此问题?

2 个回复
SO网友:WebElaine

你肯定需要设置map_meta_captrue 而不是false来创建自定义功能。

我还没有看到\'capability_type\' => [\'note\',\'notes\'] 以前创建功能的方式—可能是简写,但如果只是更改map_meta_cap 如果不起作用,您可能需要从长远的角度来解释一切:

<?php
$args = array(
    \'labels\'          => $labels,
    \'public\'          => false,
    \'show_ui\'         => true,
    \'show_in_menu\'    => true,
    \'query_var\'       => true,
    \'has_archive\'     => false,
    \'menu_position\'   => null,
    // The most crucial change: true
    \'map_meta_cap\'    => true,
    // Possibly required: spelling out every capability individually
    \'capabilities\'      => array(
        \'edit_post\'                 => \'edit_note\',
        \'read_post\'                 => \'read_note\',
        \'delete_post\'               => \'delete_note\',
        \'create_posts\'              => \'create_notes\',
        \'delete_posts\'              => \'delete_notes\',
        \'delete_others_posts\'       => \'delete_others_notes\',
        \'delete_private_posts\'      => \'delete_private_notes\',
        \'delete_published_posts\'    => \'delete_published_notes\',
        \'edit_posts\'                => \'edit_notes\',
        \'edit_others_posts\'         => \'edit_others_notes\',
        \'edit_private_posts\'        => \'edit_private_notes\',
        \'edit_published_posts\'      => \'edit_published_notes\',
        \'publish_posts\'             => \'publish_notes\',
        \'read_private_posts\'        => \'read_private_notes\'
    ),
    \'rewrite\'         => [ \'slug\' => \'note\', \'with_front\' => false ],
    \'supports\'        => [ \'editor\' ],
    \'menu_icon\'       => \'dashicons-format-aside\',
);
register_post_type( \'note\', $args );
?>
您可能还想授予自定义角色一些其他功能:

<?php
$role = get_role( \'my_custom_role\' );
// Read (front end) all post types
$role->add_cap(\'read\');
// Adjust their dashboard
$role->add_cap(\'edit_dashboard\');
// Upload files
$role->add_cap(\'upload_files\');
// See the list of users (but not manage them)
$role->add_cap(\'list_users\');
// Allow taxonomy management - Categories and custom taxonomies
$role->add_cap(\'manage_categories\');
// Use the Customizer
$role->add_cap(\'edit_theme_options\');
// Only if you really need to, allow them to paste in HTML/JS
$role->add_cap(\'unfiltered_html\');
?>
至少我通常会向自定义角色授予“读取”权限,以便他们可以看到站点的前端。

SO网友:szabo.eva.irma

我的问题是一样的,在这里对我有效:

将此行添加到功能:$role->;添加\\u cap(\'create\\u notes\');

当您修改某些内容而更改不适用时,此插件对于清除自定义规则和功能非常有用:Reset roles and capabilities

此外,您还可以尝试此插件:https://wordpress.org/plugins/capability-manager-enhanced/