We have an issue with a WP plugin that we wrote and maintain – Export User Data
A user has reported an issue that non-unique user meta data records are not being returned correctly – here
In the plugin, we export the usermeta data selected by the user – using get_users() which in turn uses WP_User_Query:
We pass a few simple arguments to get_users:
// build argument array ##
$args = array(
'fields' => 'all',
'role' => sanitize_text_field( $_POST['role'] )
);
If we inspect a WP_User object returned, the usermeta fields are not returned – for example ( object data reduced to save space ):
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 1267
[user_login] => user@email.com
...
)
[ID] => 1267
...
)
[1]...
We have tried to change the get_users args for the “fields” parameter from “all” to “all_with_meta”, however this does not seem to change the originally returned data.
At the point at which we export these user metadata rows we firstly loop over that array of WP_User objects and then echo out the individual usermeta field data ( $field comes from an array of $fields which loops outside the $users loop ):
// build row values for each user ##
foreach ( $users as $user ) {
// grab value from $user object ##
$value = $user->{$field};
}
The field data is being magically added to the $user object, even though this is not shown in the originally returned object data – however, we have no control over if it returns a single or array of values for each usermeta field.
As the data is being returned automatically, we are not controlling the selected method, which we would be able to if we used get_user_meta directly ( but we’d still have the issue of not knowing the stored data is unique or not, without running extra queries – which would be costly on large exports ).
I’m writing this all out to try and explain to others the issue, while also to help us look for answers and resolve this issue.
Update
We’ve pushed a test fix to github using a method to check for non-unique usermeta keys and return an array in the case that there are more than one matching keys