{"id":1379,"date":"2012-10-31T23:24:14","date_gmt":"2012-10-31T23:24:14","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1379"},"modified":"2012-10-31T23:24:14","modified_gmt":"2012-10-31T23:24:14","slug":"wordpress-last-insert-id-get-wpdb-last-insert-id-in-while-loop","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/wordpress-last-insert-id-get-wpdb-last-insert-id-in-while-loop\/","title":{"rendered":"WordPress Last Insert ID | Get wpdb Last Insert ID in while loop"},"content":{"rendered":"<p><strong>WordPress Last Insert ID :<\/strong>\u00a0In WordPress, at some point or the other, you may have come across a situation where you would need to get the last insert id i.e. the value of the auto-increment column as a result of insert operation performed. If you are running a normal SQL query, you would get the last insert ID using mysql_insert_id(). But do you know how to <strong>get the last inserted row ID in WordPress<\/strong>? It&#8217;s very simple &amp; the code is just 1 line. Read on to find out the solution.<\/p>\n<p><!--more--><\/p>\n<h2>In WordPress, get Last Insert ID using\u00a0wpdb \u00a0in while loop<\/h2>\n<p>Let&#8217;s assume that you are working with WordPress and that you are inserting multiple products into the database using a custom for\/foreach loop. In order to find out the last inserted row ID, you would generally use\u00a0mysql_insert_id(). So now, we would need the equivalent of the\u00a0mysql_insert_id() in WordPress using wpdb. <strong>We can get the last inserted id in WordPress using the following code<\/strong>:<\/p>\n<pre class=\"lang:php decode:true\">$last_inserted_id = $wpdb-&gt;insert_id;<\/pre>\n<p>So just place the above line of code, just below the actual wpdb query. Following is an example:<\/p>\n<pre class=\"lang:php decode:true\">$result = $wpdb-&gt;query( \"INSERT INTO $table_name ( col1, col2 ) VALUES ( '$col1', '$col1' )\");\n\n$last_inserted_id = $wpdb-&gt;insert_id;<\/pre>\n<p>So that&#8217;s how you can get the last inserted row ID value in WordPress.<\/p>\n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to <strong>get the last inserted ID in WordPress<\/strong>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress Last Insert ID :\u00a0In WordPress, at some point or the other, you may have come across a situation where you would need to get the last insert id i.e. the value of the auto-increment column as a result of insert operation performed. If you are running a normal SQL query, you would get the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,11],"tags":[30],"class_list":["post-1379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks","category-wordpress-tips-and-tricks","tag-wordpress-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-mf","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1379"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}