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.
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.
Share Improve this question edited Jan 17, 2017 at 14:26 Riyas Muhammed asked Jan 17, 2017 at 13:04 Riyas MuhammedRiyas Muhammed 1271 silver badge5 bronze badges1 Answer
Reset to default 1You can filter the_content
to make sure only people with the right cap can see it:
function filter_the_content( $content ) {
// Check post type is book
// and that the current user is not lite user
if (get_post_type() == 'book'
&& ! current_user_can( 'read_book' ) ) {
// return a message in place of the content
return 'Why do you want to see this book? You can\'t read!';
}
// otherwise, show content
return $content;
}
add_filter( 'the_content', 'filter_the_content' );
However!
This will only work for lite user, meaning that any other user (including admins) won't be able to see it from the front end.
You will have to add the read_book
capability to other roles.
Hope that's what you are looking for.