你必须过滤user_has_cap
并比较用户元字段。
以下代码基于this answer 和未测试。
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;
}