I would like to display a list of any users whose emails are detected to have email addresses from an array of domains.

$domains = array('domain1.com', 'domain2.com', 'domain3.com');

If any users match those domains they would be listed in an un-ordered list, such as:

user1@domain1.com user2@domain1.com user3@domain3.com etc

3 Answers 3


You can try below code which return object of matched records with user_email column into wordpress database users table.

        This function will return object of all match user records
        Function Location : themes/function.php
function get_domain_user($domains){
        global $wpdb; 

        //Build query for match records selection using RLIKE
        $strdomain = '';
        foreach( $domains as $domain ) : 
                if($strdomain == ''){ 
                        $strdomain = "'" . $domain;
                else {
                        $strdomain = $strdomain . "|" . $domain;
        $strdomain = 'user_email RLIKE ' . $strdomain . "'";

        //Execute query for pull all respected records...
        $strQuery = "SELECT * FROM $wpdb->users WHERE $wpdb->users.$strdomain";
        return $user_query = $wpdb->get_results($strQuery);

Call this function like:

//Email address domain array...
$domains = array('domain1.com', 'domain2.com', 'domain3.com');

//Pull records which users email address contain $domains mail address...
$user_list = get_domain_user( $domains );

If there is any issue/doubt/concern regarding this please contact me.


Not tested, but it could get you started:

function get_users_list_by_domain_array($domains)
     $final_list = [];
     foreach( $domains as $domain )
          $blog_id = get_blog_id_from_url($domain);
          if( ! $blog_id ) continue;
          $users = get_users(['blog_id'=>$blog_id]);
          $list = array_map(function($user){
               return $user->user_login.'@'.$domain;
          $final_list = array_merge($final_list,$list);

     return $final_list;


Try following code:

global $wpdb; 

$domains = array('domain1.com', 'domain2.com', 'domain3.com');
$str  =''; //Initialize string;
    if($i == count($domains)-1)
    $str.= " user_email like '%{$domains[$i]}' "; //Build sql query to match email domain
    $str.= " user_email like '%{$domains[$i]}' or ";    


$users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE $str"); //This will return list of users with matching email domain
echo "<ul>";
foreach($users as $user)
echo "<li>$user->user_login</li>"; //Print users
echo "</ul>";

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.