1

I need to check if users have the exact roles for the purpose of allow users to edit each others posts if they have a same two roles.

One role 'X' is a common one, and the other role 'Y' is a unique one - I'm generating it dynamically based on a user meta value.

The second role is the main role for what I'm trying to do, grouping users and give them access to each others posts and the first role is the role associated to manage the targeted post type.

The problem now is: users can edit all others users with role X posts , no matter they have the role y or not .... what i need is , they can only edit when they have the both roles , not only one of them . this is the code:

function get_current_user_role() {
    global $wp_roles;
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift($roles);
    return isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role] ) : false;
}

$userRole = get_current_user_role();
$contributors = get_users(array('role'=> $userRole ));
$contributors_ids = array();

foreach ($contributors as $user) {
    $contributors_ids[] =  $user->ID;
}
        $args = apply_filters( 'moe_cars_args', array(
            'post_type'           => 'cars',
            'offset'              => ( max( 1, get_query_var('paged') ) - 1 ) * $posts_per_page,
            'orderby'             => 'date',
            'order'               => 'desc',
            "author"        => implode( ',', $contributors_ids )
        ) );
1

To achieve this, you first need to get all the current user roles, X and Y(s)...

function get_current_user_roles(){
    global $wp_roles;

    $current_user = wp_get_current_user();
    $user_groups  = groups_get_user_groups( $current_user->ID );
    $user_roles   = isset( $current_user->roles['X'] ) ? array( 'X' ) : array();

    // Let's get all Y dynamic user roles...
    foreach ( $user_groups['groups'] as $id ) {
        $group        = groups_get_group( array( 'group_id' => $id ) );
        $user_roles[] = 'bp_gm_' . $group->slug . '__' . $group->id;
    }

    // Let's translate the roles...
    if ( ! empty( $user_roles ) ) {
        $translated_user_roles = array();

        // I don't get why you have to check against `$wp_roles`...
        foreach ( $user_roles as $role ) {
            if ( isset( $wp_roles->role_names[ $role ] ) ) {
                $translated_user_roles[] = translate_user_role( $role );
            }
        }
    }

    return $translated_user_roles;
}

Now that we can get_current_user_roles(), let's implement our specifications...

$user_roles = get_current_user_roles();

if ( count( $user_roles ) > 1 && isset( $user_roles['X'] ) ) {
    $args = apply_filters( 'moe_cars_args', array(
        'post_type' => 'cars',
        'offset'    => ( max( 1, get_query_var('paged') ) - 1 ) * $posts_per_page,
        'orderby'   => 'date',
        'order'     => 'desc',
        'author'    => implode( ',', get_users( array(
            'role'   => $user_roles,
            'fields' => 'ID',
        ) ) ),
    ) );
}

Hope this answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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