admin管理员组

文章数量:1022452

One of the plugins I am using generates user meta from our AD. One of the keys is the user's Manager.

As many users will have the same manager, I want to be able to group all users.

I was trying to find any way to do this, or adapt this post: getting all values for a custom field key (cross-post) but can't wrap my head around SQL database writing.

Practically, I want to be able to loop each meta manager with all the users that have that meta_value with get_users

One of the plugins I am using generates user meta from our AD. One of the keys is the user's Manager.

As many users will have the same manager, I want to be able to group all users.

I was trying to find any way to do this, or adapt this post: getting all values for a custom field key (cross-post) but can't wrap my head around SQL database writing.

Practically, I want to be able to loop each meta manager with all the users that have that meta_value with get_users

Share Improve this question asked Apr 19, 2019 at 7:27 markbmarkb 2996 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Eventually came to a solution in case anyone needs it:

function mb_get_user_meta_values( $mb_user_meta_key ) {
    if( empty($mb_user_meta_key) )
        return;

    $mb_get_users = get_users( array( 'meta_key' => $mb_user_meta_key ) );
    $mb_user_meta_values = array();
    foreach( $mb_get_users as $mb_user ) {
        $mb_user_meta_values[] = get_user_meta( $mb_user->ID, $mb_user_meta_key, true );
    }
    $mb_user_meta_values = array_unique( $mb_user_meta_values );
    return $mb_user_meta_values;
}

Then I can loop through the get_users with the meta_value

One of the plugins I am using generates user meta from our AD. One of the keys is the user's Manager.

As many users will have the same manager, I want to be able to group all users.

I was trying to find any way to do this, or adapt this post: getting all values for a custom field key (cross-post) but can't wrap my head around SQL database writing.

Practically, I want to be able to loop each meta manager with all the users that have that meta_value with get_users

One of the plugins I am using generates user meta from our AD. One of the keys is the user's Manager.

As many users will have the same manager, I want to be able to group all users.

I was trying to find any way to do this, or adapt this post: getting all values for a custom field key (cross-post) but can't wrap my head around SQL database writing.

Practically, I want to be able to loop each meta manager with all the users that have that meta_value with get_users

Share Improve this question asked Apr 19, 2019 at 7:27 markbmarkb 2996 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Eventually came to a solution in case anyone needs it:

function mb_get_user_meta_values( $mb_user_meta_key ) {
    if( empty($mb_user_meta_key) )
        return;

    $mb_get_users = get_users( array( 'meta_key' => $mb_user_meta_key ) );
    $mb_user_meta_values = array();
    foreach( $mb_get_users as $mb_user ) {
        $mb_user_meta_values[] = get_user_meta( $mb_user->ID, $mb_user_meta_key, true );
    }
    $mb_user_meta_values = array_unique( $mb_user_meta_values );
    return $mb_user_meta_values;
}

Then I can loop through the get_users with the meta_value

本文标签: pluginsGet all user metakeys and then group users by matching values