Thank you for your reply, but I don't think you have solved my problem yet.
I am attempting to set different read permissions on content based on the author's user role. My end goal is to remove Guest read access to all Member created content, when they are down-graded to a regular Subscriber.
For example:
- Guest users have read access to Events created by a "Member" user role.
- Guest users do NOT have read access to Events created by a "Subscriber" user role.
With access control and post groups I am able to remove Guests read access for all Events, and I'm able to allow Members to create Events... but I don't see how to grant Guest read access to only the Events created by Members.
If this works, I can manage all of the Member generated content access with just ONE access rule. If it doesn't work, then I will have to change every Member generated event, every time a Member changes their user role.
EDIT:
I just ran the API documentation through an AI, and it came up with this function. I believe using this code with the Custom Capabilities will achieve the restriction effect I'm after.
function restrict_guest_event_access( $allcaps, $cap, $args, $user_id ) {
if ( $cap === 'read_post' ) {
if ( isset( $args[0] ) ) {
$post_id = $args[0];
$post = get_post( $post_id );
if ( $post && $post->post_type === 'events' ) {
$author_id = get_post_field( 'post_author', $post_id );
$author = get_user_by( 'id', $author_id );
if ( $author ) {
$author_roles = (array) $author->roles;
//check if author has the professional role
if ( in_array( 'professional', $author_roles) ) {
// Check if current user (guest) has the capability to view professional events
if( ! current_user_can( 'view_professional_events' ) ) {
$allcaps['read_post'] = false;
}
} else {
// Check if current user (guest) has the capability to view general events
if ( ! current_user_can( 'view_general_events' ) ) {
$allcaps['read_post'] = false;
}
}
}
}
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'restrict_guest_event_access', 10, 4 );