user capabilities restricted to access specific pages only in wordpress

Solution:

I just installed and tried out this plugin: https://wordpress.org/plugins/advanced-access-manager/ . It should do the trick for you. However some of it you may have to pay for if you need advanced stuff.

In this plugin, go to the Settings (AAM), then in the User Roles area, press the gear beside the role you want to edit. Then navigate to the right post or page, press gear icon again and set the permissions you desire.

If you are interested in custom code, there are ways to do it that I can provide if you prefer to not use a plugin – just let me know and I can elaborate on how to do it.

EDIT: here’s some code you could put on page.php or single.php

Inside the loop, for admins only, do:

<?php 
   // First, specify only to check for permissions on certain pages/posts by ID
   if(is_page(1,2,3)||is_post(4,5,2)) {
      if(current_user_can( 'manage_options' )) { ?>
        <!-- Here, all the the_title(), the_content(), etc should go -->
        <h1><?php the_title(); ?></h1>
<?php } else { 
        echo 'Sorry, you don't have permission to view this page.'
      } 
   } else { ?>
  <!-- Display everything as normal -->
      <h1><?php the_title(); ?></h1>
<?php } ?>

If you want to test other than admin, then try 'moderate_comments' for Editor, 'publish_posts' for Author, 'edit_posts' for Contributors, and 'read' for Subscribers (this goes into the current_user_can() function). See https://codex.wordpress.org/Roles_and_Capabilities.