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


      // "lite_user" is a custom user role to manage book
    $roles = array( get_role('lite_user') );
    foreach($roles as $role) {
      if($role) {

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.

1 Answer 1


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


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.

  • Thanks, Pedro, it is working well, now I can restrict public view(guest), Actually I was looking for restriction through add_cap itself like as backend, Buth this also brilliant solution :) Commented Jan 19, 2017 at 16:11
  • 1
    Just thought of this: nother option is hooking into template_redirectto do the check and redirecting to a 404 Page. Commented Jan 19, 2017 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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