I am trying to insert a large number of user in to the wordpress db using wp_insert_user(). After inserting each user, I am trying to add an user meta for each user. I am doing like this:

$meta_value = //data from an API (e.g.: 19, 34, 1290 etc)
$user_id = wp_insert_user( $new_user_data );// it works fine
update_user_meta( $user_id, 'my_meta_key', $meta_value ); //does not work

When I run it, user get inserted, but not the meta. When I try like below, it works:

$user_id = wp_insert_user( $new_user_data );// it works fine
update_user_meta( $user_id, 'my_meta_key', '1290' ); //it works

I have over 1000 users, I need to insert, so I need to insert the meta dynamically as well. Any idea how can I overcome this? Thanks.

  • 1
    Please add debug information to your question to make answers possible. – fuxia Mar 31 '13 at 22:01
  • What is the output of var_dump( $meta_value ); ? – Johannes Pille Mar 31 '13 at 22:43
  • Anyway, I fixed it. Leaving the solution as answer, if anyone else face similar problem, it will help them. – тнє Sufi Mar 31 '13 at 23:04

I fixed it doing like below:

$meta_value = //data from an API (e.g.: 19, 34, 1290 etc)
$meta_value_string = "". $meta_value;
$user_id = wp_insert_user( $new_user_data );
update_user_meta( $user_id, 'my_meta_key', $meta_value_string );

Now it works fine.

  • For one: Note the existence of type casting and strval() in PHP. For another, that ought not be necessary anyway: the $meta_value parameter of update_user_meta() can even be assigned an object or array, which will be serialized before being written to the database. It will accept an integer just fine. The error must have been elsewhere. – Johannes Pille Apr 1 '13 at 0:44

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.