First, I second Q Studio comment: check Tom McFarlin’s post with a warning about using pre_get_posts. More specifically, pre_get_posts
works everywhere, including RSS and dashboard, and you might want to exclude your logic from such places.
That said, if I understood correctly what you are trying to do, you can achieve it by using author__in
parameter of WP_Query
in pre_get_posts
.
In fact, the developer documentation have an example:
$query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) );
Your code could be adapted like this:
add_action('pre_get_posts', 'wpse_382339_query_set_only_author' );
function wpse_382339_query_set_only_author( $wp_query ) {
global $current_user;
if( is_admin() && !current_user_can('edit_others_posts') ) {
// 2 is just a placeholder for your user.
$wp_query->set( 'author__in' , array( 2, $current_user->ID ) );
add_filter('views_edit-post', 'fix_post_counts');
add_filter('views_upload', 'fix_media_counts');
}
}
Bear in mind that this allow users to SEE both their posts and yours. But not edit.
Let me know if it helps or you need more clarification