How to restrict CPT post's fronted view only for specific user roles?
I have a CPT named 'book' and I would like to restrict front-end view of this book, like specific role users can only view this book contents from front-end, by default anyone can see CPT posts from frontend, I have tried with capabilities and map_meta_cap but not getting expected result.
CPT code:
$args = array(
'labels' = $labels,
'hierarchical' = true,
'description' = 'test book',
'supports' = array( 'title', 'editor', 'author'),
'public' = true,
'show_ui' = true,
'show_in_menu' = true,
'show_in_nav_menus' = true,
'publicly_queryable' = true,
'has_archive' = true,
'query_var' = true,
'rewrite' = true,
'capability_type' = array('book', 'books'),
'capabilities' = array(
'edit_post' = 'edit_book',
'edit_posts' = 'edit_books',
'edit_others_posts' = 'edit_others_books',
'publish_posts' = 'publish_books',
'read_post' = 'read_book',
'read_private_posts' = 'read_private_books',
'delete_post' = 'delete_book'
)
);
register_post_type( 'book', $args );
Function:
// "lite_user" is a custom user role to manage book
$roles = array( get_role('lite_user') );
foreach($roles as $role) {
if($role) {
$role-add_cap('read');
$role-add_cap('edit_book');
$role-add_cap('read_book');
$role-add_cap('delete_book');
$role-add_cap('edit_books');
$role-add_cap('edit_others_books');
$role-add_cap('publish_books');
$role-add_cap('read_private_books');
}
}
by this code, any public can view/access these books from frontend but, I want to restrict this, lite_user role users can only able to view/access books from front-end.
Topic user-access front-end user-roles capabilities custom-post-types Wordpress
Category Web