WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19540 closed defect (bug) (worksforme)

get_userdata() doesn't include meta values in 'data' property

Reported by: beautomated Owned by: otto
Milestone: Priority: normal
Severity: major Version: 3.3
Component: Users Keywords:
Focuses: Cc:

Description

The following code worked in v3.2.1 and broke in v3.3. The custom meta value for "company" is empty, returning 'N/A' instead of what it should be: 'ABC Corporation'.

update_user_meta(get_current_user_id(), 'company', 'ABC Corporation');

$users = get_users(array('role' => 'administrator'));
foreach ($users as $user) {

$user = get_userdata($user->ID);
$company = (property_exists($user, 'company') && ($val = $user->company)) ? $val : 'N/A';
echo "You work for {$company}<br />";

}

Change History (3)

comment:1 scribu2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

If you use isset( $user->company ), it will work and be compatible with both WP 3.2 and 3.3.

For more info, see #15458

comment:2 beautomated2 years ago

Thanks, Otto. This solution did work for two of our client projects. Note that I'm not comfortable with the notion of "Magic" properties, nor changes in the API without deprecation. I don't think any hidden properties are helpful to developers who don't already know about them. Nevertheless, I really appreciate the help and it did work for us. Thank you.

comment:3 scribu2 years ago

You're welcome, but I'm not Otto. :P

Note: See TracTickets for help on using tickets.