Home Forums General Issues WP_User_Query by Select Field Values


WP_User_Query by Select Field Values

  • Hey everyone. I am trying to query WP_USERS by a custom field that is a select field. I’ve mostly got it working, but i’m still having a weird issue, where results will return multiple results of the same name. Normally I would just do a “DISTINCT” addition but i’m not sure the way to do that in WordPress / ACF. Anyways here’s what I want to accomplish:

    3 Search Fields
    1 – A Dropdown of all User’s First Name & Last Name
    2. A group of checkboxes of all the users types of artwork. There’s about 80 types in here.
    3. A group of checkboxes of all the users locations (So for example the select box has the following values;
    1. North London / North Portsmouth / North Chester
    2. South Liverpool / East Liverpool
    3. Newcastle / Sunderland)

    So, let’s say someone picks the following:
    1. Sylar Gaf
    2. Painting, Airbrush
    3. North London / North Portsmouth / North Chester

    Then the database would check to see if there’s a Sylar Gaf who does Painting and Airbrush in a preassigned region of North London / North Portsmouth / North Chester. Pretty straight forward.

    I have 1 & 2 sort of working. When I try to do the above search like this:

    1. Sylar Gaf
    2. Airbrush

    I get 1 result back; Sylar Gaf. This works.

    However if I do the following search:

    1.Sylar Gaf

    I get 2 results back: Sylar Gaf, Sylar Gaf. So it’s working, but repeating. And it’s the same result if I combine number 2 (2. Airbrush, Painting). A sample request looks like this:

    	"artistFilter": "Sylar Gaf",
    	"mediumType": [
    	"action": "myfilter"

    I have the logic programmed for search 3, but it doesn’t return anything back, but I think that may be because of the info it’s retrieving from ACF?

    Anyways, hoping you folks could help out. Below is my code.



    function misha_filter_function(){
        $args = array(
            'role' => 'artist',
            'order' => 'ASC',
            'orderby' => 'display_name',
            'meta_query' => array(
        if (isset($_POST['artistFilter'])){
            $name = explode(" ",$_POST['artistFilter']);
            $args['meta_query'][] = array(
                'key' => 'first_name',
                'value' => $name[0],
                'compare' => 'LIKE'
            $args['meta_query'][] = array(
                'key' => 'last_name',
                'value' => $name[1],
                'compare' => 'LIKE'
        if (isset($_POST['mediumType'])){
            // WP_User_Query arguments
            $args['meta_query'][] = array(
                'meta_key' => 'artist_medium',
               'value'=> $_POST['mediumType'],
               'compare' => 'LIKE'
        if (isset($_POST['locationType'])){
            $args['meta_query'][] = array(
                'value' =>$_POST['locationType'],
                'compare' => 'LIKE'
        $user_query = new WP_USER_QUERY ($args);
        if (! empty($user_query->get_results())){
            foreach ($user_query->get_results() as $user){
                echo '<a href="/staging/author/' . esc_html($user->user_login) . '">' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</a>';
        } else {
            echo '<h1>No Users Found.</h1>';

    My search form:

       <form action="<?php site_url()?>/staging/wp-admin/admin-ajax.php" method="POST" id="filter">
        if ($artists){
            echo '<div class="artistDiv">';
            echo '<select name="artistFilter"><option value="">Select Artist...</option>';
            foreach ($artists as $artist){
                echo '<option value="' . $artist->first_name . ' ' . $artist->last_name . '">' . $artist->first_name . ' ' . $artist->last_name . '</option>';
            echo '</select>';
            echo '</div>';
        if ($field){
            echo '<div class="mediumDiv">';
            foreach ($field['choices'] as $choice){
                echo '<input type="checkbox" name="mediumType" value="' . $choice .'" id="'.$choice.'">';
                echo '<label for="'.$choice.'">'.$choice.'</label><br>';
            echo '</div>';
        if ($locations):
            echo '<div class="locationDiv">';
            foreach ($locations['choices'] as $location){
                echo '<input type="checkbox" name="locationType" value="' . $location .'" id="'.$location.'">';
                echo '<label for="'.$location.'">'.$location.'</label><br>';
            echo '</div>';
    <button>Apply Filters</button> 
    <input type="hidden" name="action" value="myfilter">
  • I did some looking into this and it seems that WP has a history of returning duplicate users when doing a user query.

    Sorry, I don’t have a fix for you. Most of what I’ve found says it was fixed but you’ve obviously found a case that it is not. After following some of the stuff the search turned up what I would probably to is build a list in an array of users that have already been shown and then skip the duplicates and work around the problem.

  • Hey no worries and thanks for your time. I ended up having to do a weird function that sets “DISTINCT” somehow, but it seems to work. Thanks for your time!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.