问题描述

我有一个自定义的帖子类型,我想限制访问某些角色,但是,我已经使用自定义帖子类型添加内容,现在我必须使它们受到限制。 capability_type 是’post’

'capability_type' => 'post'

内容在后台显示的很好,现在只要我添加任何功能,内容从后端消失?

我已经尝试自定义能力类型以包括复数定义来构造自己的,但一旦我删除或更改能力类型,它已经走了!

全码:

add_action( 'init', 'register_cpt_gallery' );

function register_cpt_gallery() {
$labels = array(
    'name' => _x( 'Galleries', 'gallery' ),
    'singular_name' => _x( 'Gallery', 'gallery' ),
    'add_new' => _x( 'Add New', 'gallery' ),
    'add_new_item' => _x( 'Add New Gallery', 'gallery' ),
    'edit_item' => _x( 'Edit Gallery', 'gallery' ),
    'new_item' => _x( 'New Gallery', 'gallery' ),
    'view_item' => _x( 'View Gallery', 'gallery' ),
    'search_items' => _x( 'Search Galleries', 'gallery' ),
    'not_found' => _x( 'No galleries found', 'gallery' ),
    'not_found_in_trash' => _x( 'No galleries found in Trash', 'gallery' ),
    'parent_item_colon' => _x( 'Parent Gallery:', 'gallery' ),
    'menu_name' => _x( 'Galleries', 'gallery' ),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Image galleries for teachers classes',
    'supports' => array( 'title', 'editor', 'author'),

    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,

    'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'capabilities' => array(
        'edit_post' => 'edit_gallery',
        'edit_posts' => 'edit_galleries',
        'edit_others_posts' => 'edit_other_galleries',
        'publish_posts' => 'publish_galleries',
        'read_post' => 'read_gallery',
        'read_private_posts' => 'read_private_galleries',
        'delete_post' => 'delete_gallery'
    )
);

register_post_type( 'gallery', $args );
}

我也用一个全新的自定义帖子类型进行了测试,不管能力类型如何,我得到相同的问题,即使我删除它,并添加我的自定义的一个:

'capability_type' => array('movie','movies');

最佳解决方案

在与 Justin Tadlock 指出一个有用的资源的 Magicroundabout 快速聊天后,事实证明,除非您使用 add_cap 作为角色,否则自定义帖子类型的功能实际上不存在,例如对于以下自定义帖子类型:

add_action( 'init', 'register_cpt_gallery' );

function register_cpt_gallery() {
$labels = array(
    'name' => _x( 'Galleries', 'gallery' ),
    'singular_name' => _x( 'Gallery', 'gallery' ),
    'add_new' => _x( 'Add New', 'gallery' ),
    'add_new_item' => _x( 'Add New Gallery', 'gallery' ),
    'edit_item' => _x( 'Edit Gallery', 'gallery' ),
    'new_item' => _x( 'New Gallery', 'gallery' ),
    'view_item' => _x( 'View Gallery', 'gallery' ),
    'search_items' => _x( 'Search Galleries', 'gallery' ),
    'not_found' => _x( 'No galleries found', 'gallery' ),
    'not_found_in_trash' => _x( 'No galleries found in Trash', 'gallery' ),
    'parent_item_colon' => _x( 'Parent Gallery:', 'gallery' ),
    'menu_name' => _x( 'Galleries', 'gallery' ),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Image galleries for teachers classes',
    'supports' => array( 'title', 'editor', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capabilities' => array(
        'edit_post' => 'edit_gallery',
        'edit_posts' => 'edit_galleries',
        'edit_others_posts' => 'edit_other_galleries',
        'publish_posts' => 'publish_galleries',
        'read_post' => 'read_gallery',
        'read_private_posts' => 'read_private_galleries',
        'delete_post' => 'delete_gallery'
    ),
    // as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly
    'map_meta_cap' => true
);

register_post_type( 'gallery', $args );
}

额外的功能应该添加到角色中,以便实际在后端工作,包括’administrator’ – 例如:

function add_theme_caps() {
    // gets the administrator role
    $admins = get_role( 'administrator' );

    $admins->add_cap( 'edit_gallery' );
    $admins->add_cap( 'edit_galleries' );
    $admins->add_cap( 'edit_other_galleries' );
    $admins->add_cap( 'publish_galleries' );
    $admins->add_cap( 'read_gallery' );
    $admins->add_cap( 'read_private_galleries' );
    $admins->add_cap( 'delete_gallery' );
}
add_action( 'admin_init', 'add_theme_caps');

我希望这对他人有用。

次佳解决方案

加:

map_meta_cap => true

到你的 $ args 数组。查看 here,了解更多。希望有帮助!

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。