Inside while loop of single.php you can put condition like this
if(in_category('Catetory_slug')){ //Specifying category whom we don't want to see by no logged in users
if(is_user_logged_in()){ //Checking if user logged in or not
//code inside single.php
}else{
Echo "You need to login to view this post."; //message for no logged in users
}}
This can be done for other template of theme files like we did for single.php.
UPDATE
If you want one user couldn’t see posts of another user then you can use these two functions to achieve this
- get_the_author(): To get display name of author of post
- wp_get_current_user(): To get info of logged in user
Use following inside while loop
//storing autor's display name in variable
$post_author = get_the_author();
$current_user = wp_get_current_user();
//storing autor's display name in variable
$current_user_info = $current_user->display_name;
//Chacking if bot strings (display names) are equal or not
if (strcmp($post_author, $current_user_info) == 0) {
//Your Code to display post's content
}
You can also put if condition on ID instead of display name. For that you need to make 2 changes in above code
- replace
get_the_author()
function withget_the_author_meta()
-
and replace following line of code
$current_user_info = $current_user->display_name;
with this
$current_user_info = $current_user->ID;
To know more read about this functions in detail in my given link.