Wordpress:如何在表查询中获取用户名和姓?

时间:2015-11-27 06:31:05

标签: wordpress

我的一个朋友给我写了一个小插件,它正在推出用户display_name。我此刻无法联系到他,这就是我在这里问的原因。求你的建议!

我如何得到而不是只有用户display_name的全名(名字和姓氏?)

我知道$user->last_name; $user->first_name;之类的函数,但不知道wordpress中的mysql查询。

function getEVentRSVPUsers($post_id,$event_rsvp=0){
    global $wpdb;
    $table_rsvp_event   =   $wpdb->prefix."event_rsvp as E"; 
    $query              =   "Select E.user_id,U.display_name from $table_rsvp_event 
                                LEFT JOIN ".$wpdb->prefix."users as U   
                                ON  
                                E.user_id = U.ID
                                where 
                                E.post_id = $post_id
                                AND
                                event_rsvp  =   $event_rsvp
                                group by E.user_id
                                ";  
    $results            =   $wpdb->get_results($query); 
    $userlist           =   "";
    if($results){
        $userlist       = "<ul>";
        foreach($results as $result){
            $user_nicename   =  $result->display_name;
            $userlist       .= "<li><strong>$user_nicename</strong></li>";
        }
        $userlist       .= "<ul>";
    }
    return $userlist;
}

1 个答案:

答案 0 :(得分:0)

在此代码中

   $query              =   "Select E.user_id,U.display_name from $table_rsvp_event 

将first_name和last_name添加到查询中。

   $query              =   "Select E.user_id, U.display_name, first_name, last_name from $table_rsvp_event 

并在

   foreach($results as $result){
        $user_nicename   =  $result->display_name;
        $userlist       .= "<li><strong>$user_nicename</strong></li>";
    }

将display_name更改为first_name和last_name

        $user_nicename   =  $result->first_name . " " . $result->last_name;