How to get last inserted row ID from WordPress database?

前端 未结 5 755
迷失自我
迷失自我 2020-12-04 10:58

My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I\'d like to get the ID value

5条回答
  •  感情败类
    2020-12-04 11:26

    Straight after the $wpdb->insert() that does the insert, do this:

    $lastid = $wpdb->insert_id;
    

    More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page

提交回复
热议问题