I'm not sure why you're having trouble with pre_get_posts
as that is the correct hook to use. For example, if we wanted to restrict the edit.php
screen to only show posts from authors when the current user is an author we could say:
/**
* Pre Get Posts
* Restrictions by Role
*
* @return void
*/
function wpse378774_pgp( $query ) {
// Return Early - Not Admin Panel
if( ! is_admin() ) {
return;
// Return Early - Not Main Query
} else if( ! $query->is_main_query() ) {
return;
}
$user = wp_get_current_user();
// Return Early - User not an author
if( ! in_array( 'author', (array)$user->roles ) ) {
return;
}
// Limit all queries to only show posts by author
if( $query->is_main_query() ) {
// Get all user IDs who are authors
$user_query = new WP_User_Query( array(
'role' => 'author',
'fields' => 'ID',
) );
$user_ids = $user_query->get_results();
// Set our query modifier
$query->set( 'author__in', $user_ids );
}
}
add_action( 'pre_get_posts', 'wpse378774_pgp' );
For more information please review WP_Query and WP_User_Query.