I'm using $wpdb->insert to insert data to my plugin table. How can I get the id of the new line I Just insert. Because I have a lot of users that add data to the table I can't use $wpdb->insert_id Because it's not necessary the last one.


There is no other way, but I can not see how you need any other way then this. I believe that the insert ID accessed by $wpdb is the last ID by this instance of WPDB, other inserts should not affect that but I'm not sure.



  $this_insert = $wpdb->insert_id;

| improve this answer | |
  • 2
    $wpdb is global, all queries share the same instance. – fuxia Nov 2 '16 at 10:44
  • 1
    @toscho but does a query by another user influences the insert ID you get back if they insert a query just in between your insert and read out? – Niels van Renselaar Nov 3 '16 at 13:55
  • 2
    No it doesn't. $wpdb is global, but only in this one request, it's not shared between requests of multiple users. As it's also not asynchronous, this will work as expected. – janh Oct 8 '17 at 7:31

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.