2
votes

I am trying to implement search functionality to get list of all users with same meta key and value.Created form and on submission of form passing data from form to custom template page for result but not getting user details with same meta key and meta value.

$subject_name = $_POST['sub'];
   global $wpdb; 
       $user_query = new WP_User_Query( array( 'meta_key' => 'addedsubjects_detail', 'meta_value' => $subject_name ) ); 
        $userss = $user_query->get_results(); 
       if (!empty($users)) { 
           echo '</p> <ul>'; 
           foreach ($userss as $usrer){ 
               echo '</p> <li>' . $usrer->display_name . '</li> <p>'; 
           } echo '</ul> <p>'; 
       } else { 
           echo 'No users found'; 
       } 
1

1 Answers

3
votes

There's nothing wrong with your strategy or your query. You have a few typos in your code - $userss and $usrer.

You don't need to include global $wpdb; based on this snippet alone, maybe you need it for something else.

The variable names $userss and $usrer have been replaced with $users and $user in the code below which works as intended.

$subject_name = $_POST['sub'];
$user_query = new WP_User_Query( array( 'meta_key' => 'addedsubjects_detail', 'meta_value' => $subject_name ) );
$users = $user_query->get_results();
if (!empty($users)) {
    echo '</p> <ul>';
    foreach ($users as $user){
        echo '</p> <li>' . $user->display_name . '</li> <p>';
    } 
    echo '</ul> <p>';
} else {
   echo 'No users found';
}