admin管理员组

文章数量:1287719

It's a really hectic day for me. Okay, I need to choose all users with some role, with and with some meta_data: subscriber, user_country, and user_sector for example.

In the beginning, I have several parameters: $role, $sector, $country Now I need to filter users by these parameters. At first, the task was to filter users by roles only. I did it this way:

 $role = get_option('choosed_users_role'); //I get this from the database
 $subscribers = get_users( $role );
 $emails      = array ();
 foreach ( $subscribers as $subscriber ){
     $emails[] = $subscriber->user_email;
 }

This way I get all mail of the users with the specific role. When it became necessary to add two more parameters I thought that in this way I can add other parameters for filtering.

$subscribers = get_users( $role) && get_users( $sector) && get_users(  user_country);

But it's not right I see now. What's the correct way to do this? Please help to figure out with it.

PS:

By advice Rup I tried using WP_User_Query:

$params = array(
                'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        'key'     => 'user-country',
                        'value'   => '17',
                        'type'    => 'numeric',
                    ),
                    array(
                        'key'     => 'user-sector',
                        'value'   => '602',
                        'type'    => 'numeric',
                    )
                )
            );

But not the result. I think the WP_User_Query does not work because I try to get a custom ACF meta field.

本文标签: How to get metadata (in my case mail) of users who have a certain set of metadata