read_post
is a meta capability that can be used to check if a user can read a post. It can be used, for example, like this:
if ( current_user_can('read_post', $post_ID) ) {
// do something
}
No role has this capability. It will be mapped to read_posts
, read_private_posts
or even other set of native capabilites depending on the context. Is current user the author of this post? Is this post private? What is the post status? It will do all verifications, even if the post has a custom post status.
Apparently, this type of verification, using this meta_cap, will only work when checking upon a specific user.
What about anonymous user?
Is there a way to use something similar to current_user_can('read_post', $post_ID)
that will work when there is no current user?
Of course I could do all the checking myself. Is the post public or not? and so on. But my question is, is there a way to do so whithout having to repeat all the logic that the map_meta_cap
filter for read_post
already implements so well?
Am I missing something?
read_post
is a meta capability that can be used to check if a user can read a post. It can be used, for example, like this:
if ( current_user_can('read_post', $post_ID) ) {
// do something
}
No role has this capability. It will be mapped to read_posts
, read_private_posts
or even other set of native capabilites depending on the context. Is current user the author of this post? Is this post private? What is the post status? It will do all verifications, even if the post has a custom post status.
Apparently, this type of verification, using this meta_cap, will only work when checking upon a specific user.
What about anonymous user?
Is there a way to use something similar to current_user_can('read_post', $post_ID)
that will work when there is no current user?
Of course I could do all the checking myself. Is the post public or not? and so on. But my question is, is there a way to do so whithout having to repeat all the logic that the map_meta_cap
filter for read_post
already implements so well?
Am I missing something?
Share Improve this question asked Apr 2, 2019 at 19:32 Leo GermaniLeo Germani 2211 silver badge4 bronze badges1 Answer
Reset to default 0You are not missing anything. With WordPress, you don't have control on visitors (anonymous user). The only control you have with is_user_logged_in()
to check user logged in or not. You can prevent posts from visitors.
Also, you can create a cookie or local storage based function (for control how many posts they visit) and integrate this function with is_user_logged_in()
.