根据域阵列列出所有用户

时间:2015-11-24 作者:Brandon Hubbard

我想显示其电子邮件被检测到具有来自一系列域的电子邮件地址的所有用户的列表。

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

如果任何用户与这些域匹配,他们将被列在未排序的列表中,例如:

[email protected] [email protected] [email protected] etc

3 个回复
最合适的回答,由SO网友:AddWeb Solution Pvt Ltd 整理而成

您可以尝试下面的代码,将user\\u email列中匹配记录的对象返回到wordpress数据库用户表中。

/*
        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;
                }
        endforeach; 
        $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);
}
调用此函数,如下所示:

//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 );
如果对此有任何问题/疑问/担忧,请与我联系。

SO网友:Douglas.Sesar

未经测试,但可以让您开始:

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;
          },$users);
          $final_list = array_merge($final_list,$list);

     }
     return $final_list;

}

SO网友:Aparna_29

尝试以下代码:

global $wpdb; 

$domains = array(\'domain1.com\', \'domain2.com\', \'domain3.com\');
$str  =\'\'; //Initialize string;
for($i=0;$i<count($domains);$i++)
{
    if($i == count($domains)-1)
    $str.= " user_email like \'%{$domains[$i]}\' "; //Build sql query to match email domain
    else
    $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>";