wordpress 我需要按搜索关键字列出的用户信息

tpxzln5u  于 2023-03-01  发布在  WordPress
关注(0)|答案(1)|浏览(174)

通过这个搜索脚本。访问者将使用nicename获得用户信息。这个脚本工作完美。但我需要每个用户的更多信息。像名字,姓氏,电话号码,电子邮件地址。如何写脚本?任何人都可以帮助我。

<form action="/custom-search" method="GET">

<div class="search boxes">   
<input type="text" name="ven_search_title" class="customsearch-field" placeholder="Vendor Search"> 
<input type="submit" name="search" value="Search">
</div>

</form>

<?php

 if (sanitize_text_field($_GET['ven_search_title']) && sanitize_text_field ($_GET['ven_search_title']))
    {
      $ven_search_title = sanitize_text_field($_GET['ven_search_title']);
    }
    
    global $wpdb;
    $table = $wpdb->prefix . 'users';
    $query = $wpdb->get_results("SELECT user_nicename FROM $table WHERE user_nicename LIKE '".$ven_search_title."'", ARRAY_A);
  
    foreach ($query as $value) {
         echo $value['user_nicename'];
     }

?>
sigwle7e

sigwle7e1#

您可以使用此函数获取用户信息:get_user_by()
试试下面的解决方案,让我知道它是否适合你。

<?php
$user_nicename = sanitize_text_field($_GET['ven_search_title']);
$user = get_user_by('slug', $user_nicename);

if ($user) {
    // user found, get the user information
    $user_first_name = $user->first_name;
    $user_last_name = $user->last_name;
    $user_login = $user->user_login;
    $user_email = $user->user_email;
    $user_display_name = $user->display_name;

    // do something with the user information
    echo "User First Name: " . $user_first_name . "<br>";
    echo "User Last Name: " . $user_last_name . "<br>";
    echo "User Login: " . $user_login . "<br>";
    echo "User Email: " . $user_email . "<br>";
    echo "User Display Name: " . $user_display_name . "<br>";
} else {
    // user not found
    echo "User not found.";
}
?>

你可以通过get_user_by()函数获得更多的信息,如果你需要更多的信息,请查阅该函数的文档。

相关问题