Custom post type role permissions won’t let me read

Your custom post type looks like it’s set up properly. It works on my test install. Try this instead of whatever add_role and add_cap code you’re currently using. (For testing purposes only. Don’t use it in production code, for reasons outlined below.) It’s working for me:

function prefix_set_up_supplier_role(){
remove_role( 'supplier' );
add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}
add_action( 'init', 'prefix_set_up_supplier_role' );

It’s very important to remember that adding user roles and capabilities actually saves data to the database. So if you had a version of your code before that didn’t quite work, then added something that would make it work, it might not have taken effect if there is still old data in your database. add_role() returns null if the role already exists in the database. For production code, you should actually be using the plugin activation and deactivation hooks for this stuff instead of running it every time, like this:

register_activation_hook( __FILE__, 'prefix_activate' );
function prefix_activate(){
    add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}

register_deactivation_hook( __FILE__, 'prefix_deactivate' );
function prefix_deactivate(){
    remove_role( 'supplier' );
}

Leave a Comment