0

I'm trying to write a search that makes use of several custom user meta fields I've created, but was having a tough time implementing them. Basically having the same problem this guy was having, and I solved it similarly.

              $exclude = array( '1' ); 
              $args1 = array(
                'exclude'        => $exclude,
                'search'         => '*' . $search_query . '*',
                'search_columns' => array(
                  'user_login',
                  'user_nicename',
                  'user_email',
                  'user_url',
                ),
              );

              $args2 = array(
                'exclude'    => $exclude,
                'meta_query' => array(
                  'relation' => 'OR',
                  array(
                    'key'     => 'first_name',
                    'value'   => $search_query,
                    'compare' => 'LIKE'
                  ),
                  array(
                    'key'     => 'last_name',
                    'value'   => $search_query,
                    'compare' => 'LIKE'
                  ),
                  array(
                    'key'     => 'nickname',
                    'value'   => $search_query,
                    'compare' => 'LIKE'
                  ),
                ),
              );

              // queries
              $search_query = new WP_User_Query( $args1 );
              $meta_query = new WP_User_Query( $args2 );

              $user_query = new WP_User_Query();
              $user_query->results = array_merge( $search_query->results, $meta_query->results );

              //populate total_users count for the loop to work correctly
              $user_query->total_users = $search_query->total_users + $meta_query->total_users;
              // Remove duplicate users from array
              /* final step goes here */

Now, though, when the search terms overlap with users they have in common, the queries will sometimes return the same result twice. Is there a way I can screen for duplicates?

Edit: Here's how I solved the issue.

              if (!empty($search_query->results)) {
                foreach ($search_query->results as $user) {
                    $user_info = $user->ID;
                    array_push($exclude, $user_info);
                }
              }
  • any luck with the solution I posted? – The J Feb 28 '17 at 17:55
  • Just about to get to work implementing it. Great idea. Will let you know how it goes. – MarvinLazer Feb 28 '17 at 19:46
1

Save the user IDs found in the first query, as an array, before executing the second query.

Then use that array in the exclude value, in the second query args and then run it.

UPDATE

How to put the IDs from the first query into an array, to be used in the second query.

You didn't use the fields parameter, hence the query returns everything it finds about users (all (default) Returns an array of WP_User objects)

// Put the found user IDs in an array, after executing the first user query
$exclude = array();
foreach ( $search_query->results as $user ) {
 $exclude[] = $user->ID;
}

Now you can successfully use $exclude as the value of the parameter 'exclude', as you did, in the second query.

  • Brilliant. Do you know offhand whether there's a function that exists to pull the list of user IDs in an array from a query? – MarvinLazer Feb 28 '17 at 20:00
  • I updated my answer, let me know how it goes. I didn't test it, but it should work just fine. – The J Feb 28 '17 at 20:12
  • Your solution didn't work for me, but my solution was very similar. Post updated with the info. – MarvinLazer Feb 28 '17 at 20:57
  • Updated my answer. Simply needed to add ->results (that's where the user results are). – The J Feb 28 '17 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.