1

Hey guys I've created this post type but when I click the publish button I receive the message:

You are not allowed to edit this post.

Have I done something wrong in my code to disallow anyone from publishing a post of this custom type? Here's the code:

<?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;
}
?>

Thanks in advance

  • What is your admin role when your are logging into the wp-admin? - I mean as Administrator/Editor/Author/Subscriber - What?? – Mayeenul Islam Aug 6 '13 at 22:38
  • The default capability_type is post, so you could lose that element, along with the following 'map_meta_cap'. – vancoder Aug 6 '13 at 23:01
  • It happened to me too, a few questions: 1. Did you update your permalink or made ​​flush rewrite rules after the registration of cpt? 2. You have accidentally changed the name registration for cpt and tried to update / edit / publish a post of that cpt? – iEmanuele Aug 7 '13 at 10:45
  • @MayeenulIslam My role is the admin. – Connor Aug 7 '13 at 12:42
  • @vancoder I was just keeping them there just in case I needed to change them quickly cause I was partly doing this for learning purposes. – Connor Aug 7 '13 at 12:43
1

The default for map_meta_cap is actually not false if you're also passing in a capability_type of post or page, which you are.

The following code is in 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;

So by explicitly setting it to false, you were telling it not to use the capabilities of a post or page.

By not setting it at all, you allowed the above code to set it to true.

Code just a few lines down, get_post_type_capabilities uses this flag for whether or not to use the default posts capabilities which gives you the permissions you're looking for.

0

The CPT is now working. I'm not sure why it's working but it is and here's what happened for me to make it start working. Well I saw @hawkidoki's "answer" and how he didn't use variables so I reformatted mine to look the same (below) and ran it and it didn't work until I commented out 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;
}
?>

Seeing this, I reverted back to my old code and uncommented map_meta_cap once again to get the same "You're not allowed" message. Upon commenting it out again though it works fine and my final working code is:

<?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? How could map_meta_cap be causing this issue when it's set to false? False is supposed to be the default value.

Thanks for all your help guys, especially you @vancoder!

-3

Here is a working custom_post_type declaration. Try to modify it to fit your needs :)

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');

Good luck sir!

Edit: More informations here - http://codex.wordpress.org/Function_Reference/register_post_type

  • 2
    That doesn’t answer the question. – fuxia Aug 6 '13 at 23:08
  • 1
    You neither. Think you for your contribution sir! – hawkidoki Aug 6 '13 at 23:11
  • Please add an explanation to your answer: why could your code solve the problem? – fuxia Aug 6 '13 at 23:14
  • I mean I've been following the codex the entire time, thanks anyways though. – Connor Aug 7 '13 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.