我已经创建了一个自定义帖子类型,并希望它的行为像一个页面。某些角色需要访问它,而不是帖子或页面。因此,我创建了我的自定义帖子类型,其中包含以下功能类型和功能:
function add_team_page_post_type($name, $args= array()) {
add_action(\'init\', function() use($name, $args) {
$args = array(
\'labels\' => team_page_post_type_labels( \'Team Page\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => array(\'team\', \'teams\'),
\'capabilities\' => array(
\'publish_posts\' => \'publish_teams\',
\'edit_posts\' => \'edit_teams\',
\'edit_post\' => \'edit_team\',
\'edit_others_posts\' => \'edit_others_teams\',
\'delete_posts\' => \'delete_teams\',
\'delete_post\' => \'delete_team\',
\'delete_others_posts\' => \'delete_others_teams\',
\'manage_posts\' => \'manage_teams\',
\'read_private_posts\' => \'read_private_teams\',
\'read_post\' => \'read_team\',
),
\'map_meta_cap\'=> true,
\'has_archive\' => true,
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array(\'title\',
\'author\',
\'editor\',
\'thumbnail\',
));
register_post_type( $name, $args );
});
}
add_team_page_post_type(\'team_page\', array());
并将他们分配到不同的角色,如:
add_action( \'init\', \'my_custom_capabilities\', 0);
function my_custom_capabilities(){
$role_object = get_role( \'administrator\' );
$role_object->add_cap( \'publish_teams\');
$role_object->add_cap( \'edit_teams\');
$role_object->add_cap( \'edit_team\');
$role_object->add_cap( \'edit_others_teams\');
$role_object->add_cap( \'delete_teams\');
$role_object->add_cap( \'delete_team\');
$role_object->add_cap( \'delete_others_teams\');
$role_object->add_cap( \'manage_teams\');
$role_object->add_cap( \'read_private_teams\');
$role_object->add_cap( \'read_team\');
}
经过进一步研究,我发现我需要包括
map_meta_cap()
我喜欢的功能:
add_filter( \'map_meta_cap\', \'my_map_meta_cap\', 10, 4 );
function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
/* If editing, deleting, or reading a team, get the post and post type object. */
if ( \'edit_team\' == $cap || \'delete_team\' == $cap || \'read_team\' == $cap ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
/* Set an empty array for the caps. */
$caps = array();
}
/* If editing a team, assign the required capability. */
if ( \'edit_team\' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
/* If deleting a team, assign the required capability. */
elseif ( \'delete_team\' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}
/* If reading a private team, assign the required capability. */
elseif ( \'read_team\' == $cap ) {
if ( \'private\' != $post->post_status )
$caps[] = \'read\';
elseif ( $user_id == $post->post_author )
$caps[] = \'read\';
else
$caps[] = $post_type->cap->read_private_posts;
}
/* Return the capabilities required by the user. */
return $caps;
}
这是贾斯汀·塔洛克的一篇文章
http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types问题是我只能查看我的cpt,不能编辑或删除它。我猜问题出在最后一个函数上,但我找不到太多关于它的文档。