I want to get the last auto-incremented ID's value of a particular table. I found a MySql function on the internet to do that. But I'm not sure how can I use this function in WordPress to get the value. Here is the query code I found-


Now, how I can make use of this MySql query in WordPress?

  • Is there a particular reason or goal in doing this? E.g. figuring out the post with the highest ID? What have you found so far in your research?
    – Tom J Nowell
    Commented Oct 4, 2017 at 19:35
  • Yes. It's a custom database table and I want to get the last inserted value of the auto incremented column.
    – Eh Jewel
    Commented Oct 4, 2017 at 19:40

1 Answer 1


You can write your own MySQL queries by getting the global $wpdb variable and preparing a SQL statement and then getting the results.

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT * FROM [your_table] WHERE LAST_INSERT_ID();' );
$results = $wpdb->get_results( $prepared_query );

Please, be very careful when using this, if you do it incorrectly, it could be very malicious. With what you're requesting here, you technically don't need to prepare it, but I've gotten in the habit of always preparing my statements just in case they get changed later to be more powerful.


I think I wrote my query wrong, try this:

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT LAST_INSERT_ID() FROM %s', $table_name );
$results = $wpdb->get_results( $prepared_query );

If that doesn't work, I found another resource on SO dealing with this, since the ID you mentioned was auto-incrementing, and if you are not worried about simultaneous insertions, then you maybe just need MAX(id) instead.

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT MAX(id) FROM %s', $table_name );
$results = $wpdb->get_results( $prepared_query );
  • Not working. Note: I changed the your_table with my table name. Can you check the code again?
    – Eh Jewel
    Commented Oct 4, 2017 at 19:47
  • Can you tell me how it broke or at least paste your query so I can see it?
    – socki03
    Commented Oct 4, 2017 at 19:50
  • Here is my code: $id = $wpdb->prepare( "SELECT * FROM $table_name WHERE LAST_INSERT_ID();" ); $results = $wpdb->get_results( $id ); echo $id; //output is: Array echo '<pre>'.print_r($results, 1).'</pre>'; //Returning all of the row data in a array But this function/query shuld only returen the last inserted row's auto-incremented value. From MySql doc: After inserting a row that generates an AUTO_INCREMENT value, you can get the value like this: mysql> SELECT LAST_INSERT_ID();
    – Eh Jewel
    Commented Oct 4, 2017 at 20:00
  • Alright, I edited my original answer with 2 new methods. If neither of them work, I'm not sure what else to do. Good luck!
    – socki03
    Commented Oct 4, 2017 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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