WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm in a situation where i also need to get the id of the inserted meta( meta_id for post meta and umeta_id for user meta)

For eg i add a user meta


is there a built in method of getting the umeta_id for this meta field without using custom query?

Same case for retrieving meta value with meta id. I had to use this

$sql_query = 'select umeta_id as my_key_id, meta_value from  wp_usermeta where user_id = "'.$user_id.'" and meta_key = "some_key" order by my_key_id asc';

Any better alternative?

share|improve this question

No need to get Insert id for meta field.You get key value by using this function.

$user_some_value = get_user_meta( $user_id, some_key ); 
share|improve this answer
I'm aware this is how we obtain value BUT sorry this is NOT what i need – Dipesh Kc Oct 8 '12 at 6:17
ok.You can get last insert id $lastid = $wpdb->insert_id; – dipali Oct 8 '12 at 6:24
thanks, i didn't think abt this. – Dipesh Kc Oct 9 '12 at 9:50

$wpdb->insert_id was the simple solution.


global $wpdb;
$new_meta_key_id = $wpdb->insert_id;
share|improve this answer
Can you show us a better example of the solution? – Brian Fegter Oct 9 '12 at 10:04

You don't need to do anything special here since 3.3, because add_user_meta() returns the meta ID:

$meta_id = add_user_meta( $user_id, 'some_key', $some_value, false );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.