I would like to modify the pre_user-query wp_user_query to include a meta fields from the user meta.

I have tried the two following methods, neither of which work (both return empty sets):

$query->query_from .= "  LEFT OUTER JOIN wp_usermeta AS alias ON (wp_users.ID = alias.user_id)";`

$query->query_where = str_replace(')', " OR alias.meta_value LIKE '%CH60 6RR%' )",$query->query_where);`


  $query->meta_query =  array('relation' => 'OR', 'compare' => 'LIKE', array( 'meta_key'  => 'billing_postcode', 'meta_value' => 'CH60 6RR'));

Could someone explain the correct way to return the results?

  • The second variant goes into the action pre_get_users (and is the cleaner way imho), but it's key instead of Meta_key, and value instead of Meta_value iirc. – janh Dec 29 '17 at 7:17
  • And the compare should be inside the the nested array of the key and value – Shibi Dec 29 '17 at 11:23
  • Thanks for both of your answers : Do you know why the first two queries are not working? – frank astin Dec 29 '17 at 11:41
  • OK this method is working : function custom_user_search($query) { $query->query_vars['meta_key'] = 'billing_postcode'; $query->query_vars['meta_value'] = 'CH60 6RR'; print_r($query);} – frank astin Dec 29 '17 at 11:57
  • Is possible to add multiple keys? – frank astin Dec 29 '17 at 11:58

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.