0

I have been searching the documentation on how to do this.

There is a user type manager who should be able to see the edit page of a custom post type called employees. The manager should be able to see the custom post fields but not edit them. The custom post type employee does not have it's own view. As in, an employee does not have it's own page. So that means simply giving view rights will lead them to see a blank page.

Got A Working Solution:

custom_post_type_employee.php

add_action('init',function() {
    //custom capability
    $capabilities = array(
        'publish_posts' => 'publish_employee',
        'edit_posts' => 'edit_employees',
        'edit_others_posts' => 'edit_others_employee',
        'delete_posts' => 'delete_employees',
        'delete_others_posts' => 'delete_others_employee',
        'read_private_posts' => 'read_private_employee',
        'edit_post' => 'edit_employee',
        'delete_post' => 'delete_employee',
        'read_post' => 'read_employee'
    );

    /* Setting the options for post type */
    $args = array(
        ...
        'capability_type'       => 'employee',
        'capabilities'          => $capabilities,
        ...
    );
    register_post_type('employee', $args);
});

//add capabilities to admin
$role_admin = get_role('administrator');
$admin_capabilities = array(
    'publish_employee' => true,
    'edit_employees' => true,
    'edit_others_employee' => true,
    'delete_employees' => true,
    'delete_others_employee' => true,
    'read_private_employee' => true,
    'edit_employee' => true,
    'delete_employee' => true,
    'read_employee' => true
);
foreach($admin_capabilities as $capability => $bool)
{
    $role_admin->add_cap( $capability , $bool );
}

//add capabilities to manager
$role_manager = get_role('db_manager');
$manager_capabilities = array(
    'publish_employee' => false,
    'edit_employees' => true,
    'edit_others_employee' => false,
    'delete_employees' => false,
    'delete_others_employee' => false,
    'read_private_employee' => true,
    'edit_employee' => false,
    'delete_employee' => false,
    'read_employee' => true
);
foreach($manager_capabilities as $capability => $bool)
{
    $role_manager->add_cap( $capability , $bool );
}

single-employee.php

<?php 
get_header(); 

//if not logged in correctly exit
global $current_user;
$isAdmin =  in_array('administrator', $current_user->roles);
$isManager =  in_array('db_manager', $current_user->roles);
if (!is_user_logged_in() || !($isAdmin || $isManager )) 
{ 
    die("Must be logged in to access."); 
}


echo get_field("employee_title");
echo get_field("employee_email");

get_footer(); 
?>

Special note: edit_employees MUST be set to true in order for the manager to see the page listing all the custom posts created. They however will not be able to edit one as we set edit_employee to false.

  • Did you try anything? – Sisir Sep 22 '15 at 15:03
  • Not at the moment, still researching and going through possible options to solve this. – Satbir Kira Sep 22 '15 at 15:15
0
  1. Create page template with WP_Query to show fields you want
  2. Make only managers access this page

this's the basic idea, start and if you found any problem just ask

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.