問題描述

我有一個自定義的帖子型別,我想限制訪問某些角色,但是,我已經使用自定義帖子型別新增內容,現在我必須使它們受到限制。 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。