WP_User_Query and non-unique usermeta data

The solution that we have gone with in the end uses a single call to get_user_meta passing just the $user_id – this way all user data is returned in a single query, reducing a heavy load on the DB during large user data exports.

We then run a series of checks against the returned data – including:

  • is_serialized( $value ) – to check if the data has been returned in a serialized form ( we then attempt to unserialize it – in some cases this fails where data has been stored in an incorrect form ).
  • is_array( $value ) – to check if the returned data is in fact an array

If we find that the data is returned in an array we recursively implode the array until we have a string of data to return to the export file.

I have not included specific code in this answer, but rather linked to the hosted github files ( I know this is not ideal for the future, but the parts that relate to this answer are spread out in the code ).

The exports run cleanly and do not choke-up – we’ll release the updated plugin next week.

Leave a Comment