我在用户部分创建了一些字段,现在我想通过自定义字段搜索用户。像城市名称的电话。虽然按名字和姓氏搜索工作正常,但无法通过电话或城市名称获取用户。我的自定义用户字段是“电话”和“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';
}
}
?>
我真的知道如何在查询 arg 中传递自定义用户字段。
我还想在搜索输入旁边提供一个下拉列表,用户可以选择那里的专业领域。就像他们是老师或学生一样。所以结果应该是相对的