WP_User_Query search has no results

Question

I have a location field that I want to include in my search. The first_name search is working but for some reason location searches always come up empty. I checked in the database and there is location meta_key which has a value.

$s_query = get_search_query();
$args = array(
    'search' => '*' . esc_attr( $s_query ) . '*',
    array(
        'key'     => 'first_name',
        'value'   => $search_term,
        'compare' => 'LIKE'
    ),
    array(
        'key'     => 'last_name',
        'value'   => $search_term,
        'compare' => 'LIKE'
    ),
     array(
        'key'     => 'location',
        'value'   => $search_term,
        'compare' => 'LIKE'
    )
);

Then to display the results

<?php $user_query = new WP_User_Query( $args ); ?>
<?php if ( ! empty( $user_query->get_results() ) ) {
    foreach ( $user_query->get_results() as $user ) {
        echo '<p><a href="' . home_url( '/' ) . 'member/' . $user->user_nicename . '/">' . $user->first_name . ' ' . $user->last_name . ' - ' . $user->location .  '</a><p>';
    }
} else {
    echo 'There are no users matching your search.';
} ?>

When I search by first name, it even shows the location in the result but when I search for the location I get no users.

0
, , , user10980228 7 months 0 Answers 99 views 0

Leave an answer