Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I added custom user meta information called "schools" and I want the editors to only be able to manage posts made by other users who are from the same school. How do I do that?

share|improve this question
    
Hi and welcome, Ronald. You are expected to show research efforts and/or the attempts you made to solve it yourself (see tour, How to Ask, faq). That said, I think you'll need to deal with Roles and Capabilities. Also, check this as starting point. Or maybe Multisite could be used. –  brasofilo May 14 '13 at 13:33
add comment

1 Answer

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.