Reputation: 4429
I'm adding additional metadata to users in WordPress and every time I use get_user_meta
I'm getting this error: Notice: Object of class WP_User could not be converted to int in /.../wp-includes/functions.php on line 2999
My code is
foreach( $metadata as $key => $value ) {
if ( !get_user_meta( $wcm_users[$i], $key, '' ) ) {
add_user_meta( $wcm_users[$i]->ID, $key, '' );
}
}
$metadata is an array of additional metadata I want to add, so its a basic check to see if the metadata is already added, if not, add in. Can't understand what's triggering the error. If I remove the get_user_meta
the error goes away.
Any ideas of what is going on?
Upvotes: 0
Views: 456
Reputation: 38238
get_user_meta()
takes an integer ID. You're passing it a WP_User
object. Try:
foreach( $metadata as $key => $value ) {
if ( !get_user_meta( $wcm_users[$i]->ID, $key, '' ) ) {
add_user_meta( $wcm_users[$i]->ID, $key, '' );
}
}
Also, note that the third (optional) parameter to get_user_meta()
is a boolean to indicate if you want to treat the value as a single value or an array—I'm guessing you should just be leaving it off to get the default behaviour of a single value. That's what the empty string you're passing will be doing (as an empty string is a boolean false) but it's not that obvious from your code.
Also, update_user_meta()
will add the meta if it doesn't exist, and update it if it does, which is what you say you want to do, so:
foreach( $metadata as $key => $value ) {
update_user_meta( $wcm_users[$i]->ID, $key, '' );
}
...will probably do what you want, anyway.
Upvotes: 2