无法发布自定义帖子类型-“您不被允许编辑此帖子。”

时间:2013-08-06 作者:Connor

嘿,伙计们,我已经创建了这种帖子类型,但当我单击“发布”按钮时,我收到消息:

不允许您编辑此帖子。

我的代码中是否有错误,不允许任何人发布这种自定义类型的帖子?代码如下:

<?php
function image_post_type(){
    $labels = array(
                \'name\'              =>  \'Images\',
                \'singlular_name\'    =>  \'Image\',
                \'add_new\'           =>  \'Add New\',
                \'add_new_item\'      =>  \'Add New Image\',
                \'edit_item\'         =>  \'Edit Image\',
                \'new_item\'          =>  \'New Image\',
                \'all_items\'         =>  \'All Images\',
                \'view_item\'         =>  \'View Image\',
                \'search_items\'      =>  \'Search Images\',
                \'not_found\'         =>  \'No Images found\',
                \'not_found_in_trash\'=>  \'No Images found in Trash\', 
                \'parent_item_colon\' =>  \'\',
                \'menu_name\'         =>  \'Images\'
            );

    $args = array(
                \'labels\'                =>  $labels,
                \'public\'                =>  true,
                \'exclude_from_search\'   =>  false,
                \'publicly_queryable\'    =>  true,
                \'show_ui\'               =>  true,
                \'show_in_nav_menus\'     =>  true,
                \'show_in_menu\'          =>  true,
                \'menu_position\'         =>  1,
                //\'menu_icon\'               =>  \'icon32\',
                \'capability_type\'       =>  \'post\',
                \'map_meta_cap\'          =>  false,
                \'hierarchical\'          =>  false,
                \'supports\'              =>  array(\'title\', \'editor\', \'thumbnail\', \'custom-fields\'),
                \'register_meta_box_cb\'  =>  \'add_meta_box_callback\',
                \'has_archive\'           =>  true,
                \'query_var\'             =>  true,
                \'can_export\'            =>  true
            );

    register_post_type(\'image_post\', $args);
}
add_action(\'init\', \'image_post_type\');

function add_meta_box_callback(){
    add_meta_box(\'image_variations\', \'Image Variations\', \'image_variations_callback\', \'image_post\', \'side\', \'low\');
}

function image_variations_callback(){

}

function add_image_post_type_to_query($query){
    if(is_home() && $query->is_main_query()){
        $query->set( \'post_type\', array(\'post\', \'page\', \'image_post\') );
    }
    return $query;
}
?>
提前感谢

3 个回复
最合适的回答,由SO网友:Freddy 整理而成

的默认值map_meta_cap 实际上not false 如果你也路过capability_type 属于postpage, 你就是这样。

以下代码位于Wordpress Core\'s post.php:

// Back compat with quirky handling in version 3.0. #14122
if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( \'post\', \'page\' ) ) )
    $args->map_meta_cap = true;
因此,通过将其显式设置为false, 你告诉它不要使用postpage.

通过完全不设置它,您允许上面的代码将其设置为true.

代码只是a few lines down, get_post_type_capabilities 使用此标志确定是否使用默认的POST功能,该功能为您提供要查找的权限。

SO网友:Connor

CPT现在正在工作。我不知道为什么它会起作用,但它确实起作用了,下面是我如何让它开始起作用的。我看到了@hawkidoki的“答案”以及他如何不使用变量,所以我重新格式化了我的,使其看起来相同(如下),然后运行它,直到我注释掉它才起作用map_meta_cap:

<?php
function image_post_type(){

    register_post_type(\'image_post\', array(
                \'labels\'                =>  array(
                                                \'name\'              =>  \'Images\',
                                                \'singlular_name\'    =>  \'Image\',
                                                \'add_new\'           =>  \'Add New\',
                                                \'add_new_item\'      =>  \'Add New Image\',
                                                \'edit_item\'         =>  \'Edit Image\',
                                                \'new_item\'          =>  \'New Image\',
                                                \'all_items\'         =>  \'All Images\',
                                                \'view_item\'         =>  \'View Image\',
                                                \'search_items\'      =>  \'Search Images\',
                                                \'not_found\'         =>  \'No Images found\',
                                                \'not_found_in_trash\'=>  \'No Images found in Trash\', 
                                                \'parent_item_colon\' =>  \'\',
                                                \'menu_name\'         =>  \'Images\'
                                            ),
                \'public\'                =>  true,
                \'exclude_from_search\'   =>  false,
                \'publicly_queryable\'    =>  true,
                \'show_ui\'               =>  true,
                \'show_in_nav_menus\'     =>  true,
                \'show_in_menu\'          =>  true,
                \'menu_position\'         =>  1,
                //\'menu_icon\'               =>  \'icon32\',
                \'capability_type\'       =>  \'post\',
                //\'map_meta_cap\'            =>  false,
                \'hierarchical\'          =>  false,
                \'supports\'              =>  array(\'title\', \'editor\', \'thumbnail\', \'custom-fields\'),
                \'register_meta_box_cb\'  =>  \'add_meta_box_callback\',
                \'has_archive\'           =>  true,
                \'query_var\'             =>  true,
                \'can_export\'            =>  true
            )
);
}
add_action(\'init\', \'image_post_type\');

function add_meta_box_callback(){
    add_meta_box(\'image_variations\', \'Image Variations\', \'image_variations_callback\', \'image_post\', \'side\', \'low\');
}

function image_variations_callback(){

}

function add_image_post_type_to_query($query){
    if(is_home() && $query->is_main_query()){
        $query->set( \'post_type\', array(\'post\', \'page\', \'image_post\') );
    }
    return $query;
}
?>
看到这一点,我再次返回到我的旧代码和未注释的map\\u meta\\u cap,以获得相同的“您不允许”消息。在再次对其进行评论时,虽然它运行良好,但我的最终工作代码是:

<?php
function image_post_type(){
    $labels = array(
                \'name\'              =>  \'Images\',
                \'singlular_name\'    =>  \'Image\',
                \'add_new\'           =>  \'Add New\',
                \'add_new_item\'      =>  \'Add New Image\',
                \'edit_item\'         =>  \'Edit Image\',
                \'new_item\'          =>  \'New Image\',
                \'all_items\'         =>  \'All Images\',
                \'view_item\'         =>  \'View Image\',
                \'search_items\'      =>  \'Search Images\',
                \'not_found\'         =>  \'No Images found\',
                \'not_found_in_trash\'=>  \'No Images found in Trash\', 
                \'parent_item_colon\' =>  \'\',
                \'menu_name\'         =>  \'Images\'
            );

    $args = array(
                \'labels\'                =>  $labels,
                \'public\'                =>  true,
                \'exclude_from_search\'   =>  false,
                \'publicly_queryable\'    =>  true,
                \'show_ui\'               =>  true,
                \'show_in_nav_menus\'     =>  true,
                \'show_in_menu\'          =>  true,
                \'menu_position\'         =>  1,
                //\'menu_icon\'               =>  \'icon32\',
                \'capability_type\'       =>  \'post\',
                //\'map_meta_cap\'            =>  false,
                \'hierarchical\'          =>  false,
                \'supports\'              =>  array(\'title\', \'editor\', \'thumbnail\', \'custom-fields\'),
                \'register_meta_box_cb\'  =>  \'add_meta_box_callback\',
                \'has_archive\'           =>  true,
                \'query_var\'             =>  true,
                \'can_export\'            =>  true
            );

    register_post_type(\'image_post\', $args);
}
add_action(\'init\', \'image_post_type\');

function add_meta_box_callback(){
    add_meta_box(\'image_variations\', \'Image Variations\', \'image_variations_callback\', \'image_post\', \'side\', \'low\');
}

function image_variations_callback(){

}

function add_image_post_type_to_query($query){
    if(is_home() && $query->is_main_query()){
        $query->set( \'post_type\', array(\'post\', \'page\', \'image_post\') );
    }
    return $query;
}
?>
Why though? 当map\\u meta\\u cap设置为false时,它怎么会导致此问题?默认值应为False。

感谢你们所有的帮助,尤其是你们@vancoder!

SO网友:hawkidoki

这是一个工作的custom\\u post\\u类型声明。尝试修改它以满足您的需要:)

function custom_post_example() { 
    register_post_type( \'my_post_type\',
        array(\'labels\' => array(
            \'name\' => \'my_post_type\',
            \'singular_name\' => \'my_post_type\',
            \'all_items\' => \'All my_post_type\',
            \'add_new\' => \'Add New my_post_type\',
            \'add_new_item\' => \'Add New my_post_type\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit Task\',
            \'new_item\' => \'New Task\',
            \'view_item\' => \'View Task\',
            \'search_items\' => \'Search Task\',
            \'not_found\' =>  \'Nothing found in the Database.\',
            \'not_found_in_trash\' => \'Nothing found in Trash\',
            \'parent_item_colon\' => \'Parent Task Category\'
            ),
            \'description\' => \'This is the my_post_type description\',
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => false,
            \'show_ui\' => true,
            \'query_var\' => true,
            \'menu_position\' => 5,
            \'menu_icon\' => get_stylesheet_directory_uri() . \'......./.......png\',
            \'rewrite\'   => array( \'slug\' => \'my_post_type\', \'with_front\' => true ),
            \'has_archive\' => true,
            \'capability_type\' => \'page\',
            \'hierarchical\' => true,
            \'supports\' => array( \'title\', \'author\', \'page-attributes\')
        )
    );

}
add_action( \'init\', \'custom_post_example\');
祝你好运,先生!

编辑:此处有更多信息-http://codex.wordpress.org/Function_Reference/register_post_type

结束

相关推荐

如何根据$wp_Query中的metabox值更改标记

我有一个称为“问题”的自定义帖子类型,每个问题都有一个内容页。我用发行号标记我的所有文章,因此,例如,所有发行15的文章都被标记\'issue-15\'.我有一个内容的自定义帖子模板,我使用以下代码调用同一问题编号内的所有问题帖子。$wp_query = new WP_Query( array( \'post_type\' => \'issue\', \'tag\' => \'issue-14\', \'posts_per_page\' =&g