How to get last inserted row ID from WordPress database?

前端 未结 5 754
迷失自我
迷失自我 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:35

    I needed to get the last id way after inserting it, so

    $lastid = $wpdb->insert_id;
    

    Was not an option.

    Did the follow:

    global $wpdb;
    $id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
    

提交回复
热议问题