1

I'm putting together a page that pulls User data and displays it in an attractive social-media-like format. I want to use a variable passed in through an input on the site to search multiple fields, including a couple custom ones.

This works perfectly, yielding any users whose field contents match the fields below.

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

Similarly, these arguments work perfectly alone as well.

$args = array(
  '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'
    ),
  ),
);

However, when I put them together, like so:

$args = array(
  'search'         => '*' . $search_query . '*',
  'search_columns' => array(
    'user_login',
    'user_nicename',
    'user_email',
    'user_url',
  ),

  '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'
    ),
  ),
);

It turns up zero results. I did a test and it looks like WP_User_Query is only returning users that BOTH of these filters apply to, but I want results that either/or apply to. What am I doing wrong?

In the meantime, does anyone know why the system won't let me access the display_name meta field for searches when I input it in the same way I do the arrays in meta_query()?

Thanks for any help you can give!

  • This question might be related? – birgire Feb 27 '17 at 10:07
  • This looks to be precisely what I needed. Thank you! – MarvinLazer Feb 28 '17 at 3:08

Your Answer

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

Browse other questions tagged or ask your own question.