How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Retrieve the Last Inserted Row ID in WordPress
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Retrieve the Last Inserted Row ID in WordPress
Web Development

How to Retrieve the Last Inserted Row ID in WordPress

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WordPress $wpdb->insert_id — read after $wpdb->insert or $wpdb->query INSERT
SHARE

To wordpress wpdb insert id — grab the auto-increment value of the row you just created — read $wpdb->insert_id immediately after the insert call. It’s populated after every INSERT the current connection runs, whether via $wpdb->insert() or a raw $wpdb->query('INSERT ...'). This guide covers the common case, the $wpdb->insert() vs raw-query distinction, bulk-insert semantics, and the “check return first, then read the ID” safety rule.

Contents
  • TL;DR
  • The idiomatic call
  • Always check the return value first
  • Using $wpdb->query() directly
  • Bulk inserts
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WordPress 6.5 and PHP 8.3. Originally published 2023-05-22, rewritten and updated 2026-04-23.

TL;DR

global $wpdb;

$ok = $wpdb->insert(
    $wpdb->prefix . 'my_table',
    array(
        'name'  => 'John',
        'email' => '[email protected]',
    )
);

if ( $ok === false ) {
    // handle insert failure — do NOT read insert_id
}

$last_id = $wpdb->insert_id;

The idiomatic call

$wpdb->insert(
    $wpdb->prefix . 'my_table',
    array(
        'name'  => 'John',
        'email' => '[email protected]',
    )
);

$last_id = $wpdb->insert_id;

$wpdb->insert() runs a parameterized INSERT against the named table and returns the number of rows inserted on success (1) or false on failure. After the call, the insert_id property holds MySQL’s LAST_INSERT_ID() for this connection — the auto-increment value of the new row.

No extra round trip, no second SELECT — MySQL returns the value as part of the INSERT result packet, and $wpdb exposes it on the property.

Always check the return value first

$ok = $wpdb->insert( $wpdb->prefix . 'my_table', array(
    'name'  => $name,
    'email' => $email,
) );

if ( $ok === false ) {
    return new WP_Error( 'db_insert_failed', $wpdb->last_error );
}

$last_id = $wpdb->insert_id;

If the insert fails — unique-constraint violation, dropped connection, NOT NULL column missing — $wpdb->insert_id still holds whatever value the previous successful insert left there. Reading it without checking the return value can make the code act on a stale ID. The $wpdb->last_error property is the MySQL error string you’d want to log.

wordpress wpdb insert_id — wpdb insert then read insert_id property, always check the return value first

Using $wpdb->query() directly

$sql = $wpdb->prepare(
    "INSERT INTO {$wpdb->prefix}my_table (name, email, created_at)
     VALUES (%s, %s, NOW())",
    $name,
    $email
);

$ok = $wpdb->query( $sql );

if ( $ok === false ) {
    return new WP_Error( 'db_insert_failed', $wpdb->last_error );
}

$last_id = $wpdb->insert_id;

The same insert_id property works when you’ve run the INSERT via $wpdb->query() (for expressions $wpdb->insert() can’t handle — NOW(), UUID(), subqueries). Reach for $wpdb->insert() when the values are simple; for SQL expressions in column values, fall back to query() + prepare().

Bulk inserts

$wpdb->query(
    "INSERT INTO {$wpdb->prefix}my_table (name, email) VALUES
        ('John', '[email protected]'),
        ('Jane', '[email protected]'),
        ('Mike', '[email protected]')"
);

$first_id = $wpdb->insert_id;                // e.g. 42
$rows     = $wpdb->rows_affected;            // 3
// Derived IDs: 42, 43, 44
$all_ids  = range( $first_id, $first_id + $rows - 1 );

MySQL returns the ID of the first row in a multi-row INSERT. The other IDs are contiguous — assuming no other inserts from a concurrent connection interleaved — and you can derive them from $wpdb->rows_affected. Don’t run N separate single-row inserts just to collect N IDs; that’s N round trips for no reason.

Caveat: if your table has innodb_autoinc_lock_mode = 2 (the default on MySQL 8.0+ in some deployments), the IDs may not be strictly contiguous across concurrent inserts. For absolute certainty, wrap the bulk insert in a transaction and re-SELECT the rows by a batch identifier.

Frequently asked questions

What’s the shortest wordpress wpdb insert id call?

Read $wpdb->insert_id right after $wpdb->insert(...) returns. The property holds the auto-increment value of the last INSERT the current connection performed — same semantics as MySQL’s LAST_INSERT_ID(). No extra query needed.

Is mysql_insert_id() the same thing?

It was, back in the mysql_* PHP extension days. That extension was removed in PHP 7, so mysql_insert_id() no longer exists. WordPress’s $wpdb->insert_id is the modern replacement — it wraps mysqli_insert_id() internally and gives you a WordPress-idiomatic API.

Can I use $wpdb->insert_id after $wpdb->query()?

Yes — it’s populated after any INSERT the connection runs, whether via $wpdb->insert(), $wpdb->query('INSERT ...'), or a prepared statement you execute manually. The rule: the property reflects the most recent INSERT, so read it immediately; a later non-INSERT query (SELECT, UPDATE) doesn’t overwrite it, but the next INSERT will.

What about bulk inserts — which ID do I get?

MySQL returns the ID of the first row in a multi-row INSERT. If you inserted 10 rows with auto-incrementing IDs starting at 42, $wpdb->insert_id is 42, and the other nine IDs are 43–51 (assuming no gaps). Don’t call ten separate single-row inserts to “get each ID” — that’s ten round trips for no reason. The first ID + row count is enough to derive the rest.

Does the property survive a database error?

No. If $wpdb->insert() fails (false return), $wpdb->insert_id holds whatever value the previous successful insert left there. Always check the return value before trusting the ID: if ($wpdb->insert(...) === false) { return new WP_Error(...); }. Otherwise you might end up acting on a stale ID.

Related guides

  • How to Prepare a %LIKE% SQL Statement in WordPress — the companion $wpdb->prepare pattern for reads.
  • How to Search Users by Multiple Fields in WordPress — built-in class for searches instead of raw $wpdb.
  • How to Order Posts by Meta Value in WordPress — another WP_Query vs raw SQL decision.
  • How to Retrieve the Last Inserted ID in Laravel — the Laravel-Eloquent equivalent of this pattern.

References

WordPress developer reference for $wpdb->insert and $wpdb->insert_id: developer.wordpress.org/reference/classes/wpdb/insert.

TAGGED:mysqlphpsqlwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WordPress prepare LIKE SQL — %s placeholder + % wildcards in the value How to Prepare a %LIKE% SQL Statement in WordPress
Next Article WordPress disable revision and autosave — wp-config constants and wp_print_scripts dequeue How to Disable Revisions and Autosave in WordPress
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

WooCommerce homepage filter to hide out of stock products
Web Development

Hide Out of Stock Products from Homepage in WooCommerce (Keep Them Visible Elsewhere)

5 Min Read
Laravel Carbon not found error — namespace import fix
Web Development

Fix “Class Carbon not found” in Laravel

6 Min Read
Laravel run project from GitHub — git clone through artisan serve pipeline
Web Development

How to Run a Laravel Project from GitHub

8 Min Read
WordPress logged-in menu swap — register_nav_menus + wp_nav_menu with is_user_logged_in ternary
Web Development

How to Display Different Menus to Logged-In Users in WordPress

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?