title: Writing a custom mysql query to order wordpress users by a custom user meta field
body: <p>Basically, I need to order a list of WordPress users by the city they live in.  I've got a custom user meta field for city, and I've got the query working properly, but the query lists everyone who hasn't filled out a city at the beginning since it places blank fields at the beginning of the order.</p>
  
  <p>What I need is to figure out how to only select and display users who have given a value other than blank in the city field.  Unfortunately, I've found myself stumped.</p>
  
  <p>Any thoughts on how to do this?  Also, if anyone knows a way to orderby a custom user meta field using wp_user_query as opposed to this mess, I'm all ears.</p>
  
  <pre><code>$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $limit = 10;
  $offset = ($paged - 1) * $limit;
  $key = 'city';
  
  $sql = "SELECT SQL_CALC_FOUND_ROWS {$wpdb-&gt;users}.* FROM {$wpdb-&gt;users}
  
  INNER JOIN {$wpdb-&gt;usermeta} wp_usermeta ON ({$wpdb-&gt;users}.ID = wp_usermeta.user_id)
  INNER JOIN {$wpdb-&gt;usermeta} wp_usermeta2 ON ({$wpdb-&gt;users}.ID = wp_usermeta2.user_id)
  WHERE 1=1
      AND wp_usermeta.meta_key = 'wp_capabilities'
      AND CAST(wp_usermeta.meta_value AS CHAR) LIKE '%\"subscriber\"%'
      AND wp_usermeta2.meta_key = '$key'
  ORDER BY wp_usermeta2.meta_value ASC
  LIMIT $offset, $limit";
  
  $site_users = $wpdb-&gt;get_results($sql);
  
  $found_rows = $wpdb-&gt;get_var("SELECT FOUND_ROWS();");
  
  foreach ($site_users as $site_user) { 
      // user info here
  }
  </code></pre>