Title: User Query Doesn&#039;t work?
Last modified: August 21, 2016

---

# User Query Doesn't work?

 *  Resolved [Seth Carstens](https://wordpress.org/support/users/sethcarstens/)
 * (@sethcarstens)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/)
 * I tried to use the following, which from WP documentation should show all the
   fields of all “subscribers”. However, I’m not sure your plugin is properly extending
   the user profiles now… because this code does NOT give me any of your custom 
   meta data… Can you please indicate if this is true? If so, can you tell me how
   to query the custom meta data of all users by filtering them with a query? For
   example, our list has NFL teams, I want “all users with NFL team Arizona Cardinals”,
   or better yet, “all users with NFL team != None”.
 *     ```
       //get all users as objects that are subscribers
       	$args       = array('role' => 'subscriber', 'fields' => 'all_with_meta');
       	$user_query = new WP_User_Query($args);
       	if (!empty($user_query->results)) {
       		echo json_encode($user_query->results);
       	} else {
       		echo 'No users found.';
       	}
       ```
   
 * [http://wordpress.org/plugins/user-meta-manager/](http://wordpress.org/plugins/user-meta-manager/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173847)
 * You could do something like this –
 *     ```
       global $wpdb;
       $data = $wpdb->get_results("SELECT a.*, b.* FROM $wpdb->usermeta a, $wpdb->users b WHERE a.user_id = b.ID AND meta_key = 'NFL_team' AND meta_value != 'None'");
       foreach($data as $d):
          $user = $d->user_nicename;
          echo $user . '<br />';
       endforeach;
       ```
   
 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173854)
 * In the future, I will be extending the PHP API for this plugin to include functions
   like this.
 *  Thread Starter [Seth Carstens](https://wordpress.org/support/users/sethcarstens/)
 * (@sethcarstens)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173864)
 * So the answer is, you don’t properly extend the user profiles so that the inherent
   WP_User_Query works?
 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173868)
 * When coded properly, the WP_User_Query class works just fine. I was simply offering
   an alternative method.
 *  Thread Starter [Seth Carstens](https://wordpress.org/support/users/sethcarstens/)
 * (@sethcarstens)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173870)
 * Indeed, then I’m wondering why the example from the Codex does not return any
   values for User Profile Custom Meta Data, as it claims it should.
 * [http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters)
 * [http://codex.wordpress.org/Class_Reference/WP_User_Query#Return_Fields_Parameter](http://codex.wordpress.org/Class_Reference/WP_User_Query#Return_Fields_Parameter)
 * I was intending on filtering results using built-in query objects, just like 
   you do with query posts, but I cannot get it to return any of the values created
   by UMM plugin. Any ideas?
 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173874)
 *     ```
       $args = array(
          'meta_key' => 'NFL_team',
          'meta_value' => 'None',
          'meta_compare' => '!=',
          'role' => 'Subscriber',
          'fields' => 'all_with_meta'
       );
       $user_query = new WP_User_Query( $args );
       if ( ! empty( $user_query->results ) ) {
       	foreach ( $user_query->results as $user ) {
                  //print_r($user);
       	   echo '<p>' . $user->NFL_team. '</p>';
       	}
       } else {
       	echo 'No users found.';
       }
       ```
   
 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4173875)
 * [http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters)
 *  Thread Starter [Seth Carstens](https://wordpress.org/support/users/sethcarstens/)
 * (@sethcarstens)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4174018)
 * Wow, I must apologize. I was trying to use var_export to get a list of the data
   compiled in this object, but apparently this is part of that “magic constants”
   or whatever they call it, and it cannot be printed this way… Your suggestion 
   works just fine, but now I don’t have a way to iterate through the meta fields.
   Any idea how to dump all the objects values?If not, no sweat, I appreciate you
   taking support this far.
 *  Plugin Author [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * (@jason-lau)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4174019)
 * The example I posted previously joins the wp_users and wp_usermeta tables on 
   the user id fields, and the resulting data is very easy to traverse. I recommend
   using this method.
 *     ```
       global $wpdb;
       $data = $wpdb->get_results("SELECT a.*, b.* FROM $wpdb->usermeta a, $wpdb->users b WHERE a.user_id = b.ID AND meta_key = 'NFL_team' AND meta_value != 'None'");
       foreach($data as $d):
          $user = $d->user_nicename;
          echo $user . '<br />';
       endforeach;
       ```
   
 * I’m fairly certain you will use fewer resources this way also.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘User Query Doesn't work?’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/user-meta-manager.svg)
 * [User Meta Manager](https://wordpress.org/plugins/user-meta-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/user-meta-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/user-meta-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/user-meta-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/user-meta-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/user-meta-manager/reviews/)

 * 9 replies
 * 2 participants
 * Last reply from: [Jason Lau](https://wordpress.org/support/users/jason-lau/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/user-query-doesnt-work/#post-4174019)
 * Status: resolved