Change WordPress user roles and capabilities › Forums › How to or FAQ › Defining Posts View Access in php › Reply To: Defining Posts View Access in php
11/01/2021 at 12:53
#7255
Vladimir
Keymaster
OK. I made own test.
Set for the post ID=3267 at the post editor these values:
– View Access: Prohibit view;
– For Users: All visitors (logged in or not);
– Action: Return HTTP 404 error.
As a result the post is not accessible for not logged-in and logged-in users.
Then I added the code:
add_filter( 'ure_content_view_access_data_for_role', 'my_content_view_access_data_for_role', 10, 2 );
function my_content_view_access_data_for_role( $restriction, $role_id ) {
if ( $role_id=='author' ) {
$restriction['access_model'] = 2;
$restriction['access_error_action'] = 1;
$restriction['data']['posts'] = array( 3267 ); // posts/pages ID list
}
return $restriction;
}
The post is not accessible for not logged-in and logged-in users with any role except administrator and author.