How To Get Last Insert Id In wpdb WordPress?

In PHP Get Last Insert Id In wpdb WordPress, when you go with Database $wpdb query builder then you can use mysql $wpdb->insert_id that will insert a record and then return Database last inserted record id.

we can get last insert id in four way to into WordPress projects. WordPress provide four $wpdb get row function to get current saved record id as $getLastInsertedId = $wpdb->insert_id.

If you have experience with PHP Project developing then you know we almost need to get last id. also you can need to must added top on function global $wpdb; so if you need to get last insert id then you can get $getLastInsertedId = $wpdb->insert_id method in WordPress project. here We will give you four way to get last inserted id or object in WordPress project.

wordpress insert query,wordpress update query,insert data in wordpress,wpdb get row,wordpress insert array to database,how to get last inserted data in mysql using php,how to get last inserted record in mysql,how to insert data in wp_usermeta table in wordpress
wordpress wpdb get last insert id

Get last inserted id in WordPress

WordPress insert record into table and get last insert Id

Read Also:  Calculate post read time using PHP Example

In WordPress Example, Record insertion into Database table is easy simple.also you can read my prev Articles for Get User ID in WordPress

  • Make an Object of $wpdb;
  • Set the data in an Array.
  • Set the Database table name.
  • Use $wpdb insert methods to insert save record.
  • In $wpdb insert methods, first Parameter is DB table name as well as Second Parameter is array.

Whenever you perform PHP Scripts an INSERT or UPDATE Query on database MySQL table with an AUTO_INCREMENT column then you get the last insert id of last insert or update query.In WordPress, you use $wpdb->insert_id to get last inserted record id.

See Example Below: wordpress insert query

In this example learn to wordpress insert array to database

global $wpdb;
$dbTblName = $wpdb->prefix . "users";
$user_details=array( 
'user_login' => 'lovesign',
'user_pass'=>'[email protected]',
'user_nicename' => 'Preamvati', 
'user_email' => '[email protected]',
'user_registered' => date('Y-m-d H:i:s',time()),
'user_status' => '1',
'display_name' => 'Parag Shukla');

//Insert Into MySQL Database
$wpdb->insert( $dbTblName, $user_details);

//wordpress get the last inert Id
$getLastInsertedId = $wpdb->insert_id; 
print_r($getLastInsertedId);

Last insert id in WordPress

$getLastInsertedId = $wpdb->insert_id;
print_r($getLastInsertedId);

using $wpdb->insert_id

just following the actual simple wpdb query as well as insert data in wordpress

Read Also:  Hide Uncategorized category from WooCommerce

<?php

  $wpdb->insert("QUERY");
  $this_insert = $wpdb->insert_id;

//Example

$result = $wpdb->query( "INSERT INTO $dbTblName ( fname, lname ) VALUES ( 'fname', 'rakhi )");

$getLastInsertedId = $wpdb->insert_id;
print_r($getLastInsertedId);
?>

insert data in wp_usermeta table in wordpress

SQL query

INSERT INTO `databasename`.`wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES ('4', 'websitename', MD5('websitename'), 'Your Name', '[email protected]', 'https://www.google.com/', '2021-08-09 00:00:00', '', '0', 'Divyesh');
 
 
INSERT INTO `databasename`.`wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '4', 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
 
 
INSERT INTO `databasename`.`wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '4', 'wp_user_level', '10');

Web Programming Tutorials Example with Demo

Read :

Read Also:  PHP Get ID of Last Inserted Record

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to get last inserted row ID from WordPress database?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.