I am trying to show bio and other custom metadata for my users in a page. So, i want to query the user metadata table for it. I have tried but did not find any functions for that.

What i am looking is getting all info doing a single database call.

This is what I have done so far:

    $wp_user_search = new WP_User_Query( array( 'role' => 'agent', 'fields' => 'all_with_meta') );
    $agents = $wp_user_search->get_results();

foreach($agents as $agent){


    object(WP_User)#235 (7) { ["data"]=> object(stdClass)#233 (10) 
{ ["ID"]=> string(1) "2" ["user_login"]=> string(8) "test" ["user_pass"]=> string(34) "$P$BmGqM9h7Ct7fasdfsdf5bdBUnTIYeW.wI4AUIZ0" ["user_nicename"]=> string(8) "prionkor" ["user_email"]=> string(16) "sisir@tutrem.com" ["user_url"]=> string(26) "http://barnesandklatt.com/" ["user_registered"]=> string(19) "2011-12-19 13:17:27" ["user_activation_key"]=> string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(8) "prionkor" } ["ID"]=> int(2) ["caps"]=> array(1) { ["agent"]=> string(1) "1" } ["cap_key"]=> string(15) "wp_capabilities" ["roles"]=> array(1) { [0]=> string(5) "agent" } ["allcaps"]=> array(2) { ["read"]=> bool(true) ["agent"]=> string(1) "1" } ["filter"]=> NULL } 

But it seems i am not getting all meta data Bio and other custom ones i have added (like twitter, facebook, phone etc.)

Please let me know if you have any advice. Thanks!


WP_User objects have some magic methods which do allow you to access any custom field:

foreach ( $agents as $agent ) {
  var_dump( $agent->bio );
  var_dump( $agent->get( 'bio' ) );

The two are equivalent.

More info: http://scribu.net/wordpress/the-magic-of-wp_user.html

  • Thanks! when i use get() is it calling the database again or just making the data available to use? – Sisir Dec 20 '11 at 8:10
  • 1
    In both cases, it calls get_user_meta(), which hits the database only once. – scribu Dec 20 '11 at 12:32
  • is it possible to make the order random on WP_User_Query ? currently doing it using shuffle(); on result array. – Sisir Dec 20 '11 at 18:38
  • That's a topic for a new question. – scribu Dec 21 '11 at 0:35

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.