• I’m in the process of building out a frontend AJAX search and filter page for users, that I need to search and filter results by a variety of custom meta fields. I’ve used ACF to create the meta fields in the database, and have an AJAX implementation working that will collect the data from the form and pass it to my PHP callback function. Only thing is, regardless of how I craft the query arguments for the search, I’m not getting results returned from the database. I’ve echoed out all the values that I’m passing via AJAX to make sure that they’re coming through (try a search on the attached page link, and you’ll see the big list of values appear before the search results), and I’ve made sure all the form values match the meta values that are in the user meta database fields. Some of the fields contain array data, and some just a regular string. I feel like I’ve tried every comparison or matching rule in the codex. Why are results not returning?

    Here’s my query arguments that are in the PHP callback:

        //Get Search and filter values from Ajax
          
         $mediator_search = $_POST['mediator_search'];
         $designations = $_POST['designations']; 
         $region = $_POST['region'];
         $languages = $_POST['languages'];
         $hourly_rates = $_POST['hourly_rate'];
         $associate = $_POST['associate'];
         $sliding_scale = $_POST['sliding_scale'];
         $indigenous_mediator = $_POST['indigenous_mediator'];
    
         //Dynamically create variables from form values that contain arrays - each created variable will be formatted 'designation_[number]'
    
          $designation_count = 1;
          foreach ($designations as $designation) {
              $des = 'designation_'.$designation_count;
              $$des = $designation; 
              $designation_count++;
          }
    
    $args = array(
                 'role'       => 'mediator',
                 'number'     => $per_page,
                 'paged'      => $paged,
                 'order'      => 'ASC',
                 'meta_query' => array(
                    'relation' => 'AND',
                    array(
    // Values in this meta field are an array
                       'key' => 'designations',
                       'value' => array ( $designation_1 ),
                       'compare' => 'IN'
                    ),
                    array(
                         'key' => 'region',
                         'value'   => $region,
                         'compare' => 'LIKE'
                     ),
            
                     array(
                         'key' => 'associate',
                         'value'   => $associate,
                         'compare' => '='
                     ),
                     array(
                         'key' => 'sliding_scale',
                         'value'   => $sliding_scale,
                         'compare' => '='
                     ),
    // The value in this meta field is either a 1, or nothing
                     array(
                         'key' => 'indigenous_mediator',
                         'value'   => $indigenous_mediator,
                         'compare' => '='
                     ),
                     // Trying to search keywords from a search form field here
                    array(
                         'key' => 'first_name',
                         'value'   => $mediator_search,
                         'compare' => 'LIKE'
                     ), 
                    array(
                         'key' => 'last_name',
                         'value'   => $mediator_search,
                         'compare' => 'LIKE'
                     ),  
                    array(
                         'key' => 'region',
                         'value'   => $mediator_search,
                         'compare' => 'LIKE'
                     ),      
                 )
              );

    Thanks in advance, and please let me know if any other details are needed!

    • This topic was modified 9 months, 1 week ago by pmoignard.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Shall I assume this is using the WP_User_Query class? I recommend looking at the resulting SQL to see how WP_User_Query composed it. There’s something about it that is not the way you think it should be. It’s in WP_User_Query’s request prop, but you might find it easier by using the Query Monitor plugin and finding the query’s SQL that way.

    I suspect the problem lies within the array data. What’s the nature of the individual array elements, both as saved and as submitted by the form? What you have for these might be incompatible with SQL IN logic. This part of your query might need a different approach.

    Beyond the array aspect, are you sure there’s at least one user who matches all of the submitted data?

    You also have a lot of LIKE criteria. LIKE queries are very resource intensive and should be avoided if at all possible. I’m not convinced they are appropriate for how you are using them. You’re better off managing the form input to ensure the correct names are submitted than relying upon LIKE queries. For example, the fields could be cascading style dropdown selections. The dropdowns would be populated dynamically via Ajax based on current form values so only valid values are offered. This style is often seen when searching for car parts. First the make is selected. Then the model field only reflects the current make. Then the next field would only reflect selections for the current model; and so on down the line to a specific part.

    Thread Starter pmoignard

    (@pmoignard)

    Thanks @bcworkz.

    I discovered the issue. There were a bunch of null values being passed into the query, and that was messing up the search. The solution was to conditionally add new meta_query arrays to the $args only if the value from the $_POST existed, which ensures that there are never any null values.

    I had read that when searching a field that contains a serialized array, that “LIKE” is the best operator for that. Is there a better approach for that situation?

    Moderator bcworkz

    (@bcworkz)

    Name and region meta data are saved as arrays? Then LIKE is a reasonable operator. Without understanding your data schema it’s difficult to suggest a better approach.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Help with WP_User_Query user meta field search and filter’ is closed to new replies.