You have to filter user_has_cap
and compare the user meta fields.
The following code is based on this answer and not tested.
add_filter( 'user_has_cap', 'wpse_99393_filter_cap', 10, 3 );
/**
* Allow editing others posts only for editors from the same school.
* Administrators can still edit those posts.
*
* @wp-hook user_has_cap
* @param array $allcaps All the capabilities of the user
* @param array $caps [0] Required capability ('edit_others_posts')
* @param array $args [0] Requested capability
* [1] User ID
* [2] Post ID
* @return array
*/
function wpse_99393_filter_cap( $allcaps, $caps, $args )
{
// Not our capability
if ( 'edit_post' !== $args[0] && 'delete_post' !== $args[0] )
return $allcaps;
$post = get_post( $args[2] );
// Let users edit their own posts
if ( (int) $args[1] === (int) $post->post_author )
$allcaps[ $caps[0] ] = TRUE;
// editor meta field is set and not empty
if ( ! $editor_school = get_user_meta( $args[1], 'school', TRUE ) )
return $allcaps;
// author meta field is set and not empty
if ( ! $author_school = get_user_meta( $post->post_author, 'school', TRUE ) )
return $allcaps;
if ( $author_school === $editor_school )
$allcaps[ $caps[0] ] = TRUE;
return $allcaps;
}