I have created some field in user section, Now i want to search user by custom field. Like Phone of City Name. While search by first name and last name is working fine by could not get user by phone or city name. My custom user field is "phone" and "pin_code"
<?php if( $_GET['s'] ) {
$search_term = sanitize_text_field( stripslashes( $_GET['s']));
// WP_User_Query arguments
$args = array (
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => $_phone,
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if ( ! empty( $authors ) ) {
// loop through each author
foreach ( $authors as $author ) {
// get all the user's data
$author_info = $author->ID; ?>
<div ><?php
$phone = get_field('phone', 'user_'. $author_info );
$adderess = get_field('adderess', 'user_'. $author_info );
$adderess_2 = get_field('adderess_2', 'user_'. $author_info );
$zip = get_field('pin_code', 'user_'. $author_info );
$bio = get_field('coach_bio', 'user_'. $author_info );
$category = get_field('category', 'user_'. $author_info );
if( get_field('avatar', 'user_'. $author_info ) ) {
$avatar = get_field('avatar', 'user_'. $author_info );
}
else {
$avatar = 'http://0.gravatar.com/avatar/0c12cfa22b6d6f53d7701858ecc3b67e?s=96&d=mm&r=g';
}
?>
</div>
<div class="member">
<h2 class="member-name"><a href="<?php echo get_author_posts_url($author_info); ?>"><?php echo $author->display_name; ?></a></h2>
<div class="user">
<?php $user = get_userdata( $author_info );
$user_roles = $user->roles;
if ( in_array( 'pms_subscription_plan_11', $user_roles, true ) ) {
echo '<img src="http://lifecoachnearme.co.uk/wp-content/uploads/2020/01/policy-badge.png" class="badget">';
}
?>
<div class="article_left">
<a href="<?php echo get_author_posts_url($author_info); ?>"><?php echo '<img src="'.$avatar.'" >'; ?></a>
</div>
<div class="article_right">
<div class="profile-display-address">
<div class="col-6"><?php echo $adderess; ?></div>
<div class="col-6"><?php echo $adderess_2; ?></div>
<div class="col-6"><?php echo $zip; ?></div>
</div>
<div class="contact-form contact-links"><a href="#">Contact me</a></div>
</div>
<div class="button-profile"><a class="button whitebg" href="<?php echo get_author_posts_url($author_info); ?>">View profile</a></div>
</div>
<?php }
echo '</ul>';
}
else {
echo 'No authors found';
}
}
?>
I really done know how to pass custom user field in query arg.
Also i want to give a dropdown next to the search input that user can select there area of expertise. Like they are teacher or student. So result should be relative