At a higher level, there truly isn’t any means to know if the php/db connector will return the correct id by relying on $wpdb->insert_id. The only way to be 100% sure is to have add a key you know will be unique (and indexed as such); you can then retrieve the id by querying the table against that unique key.
The reason I write this is, if a trigger function adds rows from within the DB, or more importantly if a plugin hooks in wpdb in a such a way that it messes around with inserts alongside the one you’re doing (e.g. for logging), you’re not safe.
With that in mind, hardly anyone in the WP community (and most other communities, for that matter) has the slightest idea of what a DB can do, and treats the thing as if it’s a bunch of huge arrays. So you’re very safe to rely on it in practice.
Related Posts:
- Using wpdb to connect to a separate database
- WPDB Insert or if exists Update
- get_results using wpdb
- Get error messages when $wpdb->insert() returns false?
- Detecting errors generated by $wpdb->get_results()
- Does dbDelta delete columns as well?
- wpdb update add current timestamp not working
- wpdb->insert multiple record at once
- WordPress Unit Testing – Cannot Create Tables
- Why does $wpdb return strings for mysql integer values?
- $wpdb->prepare() warning in WordPress 3.5
- Inserting data into custom tables
- How does $wpdb handle COUNT(*)
- How might I retrieve a featured post image from an external WP site and display it as a link back?
- WordPress database error: [Query was empty] [closed]
- Export wordpress table to excel
- How to close wpdb connection? [duplicate]
- Inserting Post Meta From SQL
- Why does dbDelta() not catch MysqlErrors?
- What causes the “max_user_connections” warning on WordPress frontend?
- $wpdb variable throw this error Call to a member function get_results() on a non-object in
- How to execute mulitple statement SQL queries using $wpdb->query?
- Database slowdown after update to 3.4.1
- When and why should I use $wpdb different return types?
- How wordpress accesses its own database
- Error connecting to DB in /wp-includes/wp-db.php on line 1538 [closed]
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- Get data from database using $WPDB
- How to use a different database to list and manage comments in the backend
- Getting all the users who have author privilege
- Multiple postmeta with same name for one post in wp_postmeta table
- WP Database Table to CSV file
- Cannot update custom database table row
- Show last modified date of database
- Extracting the post_id via the wp_insert_post action (external db query)
- WordPress database error – Error in SQL syntax – I can’t identify any error?
- Modify Database in Multi-Site wp_usermeta table
- Multiple wp_options tables to share content across installs
- Advanced SELECT query with condtional statements
- $wpdb doesn’t like to store arrays
- Select two sums with single get_var statement
- WordPress choose wrong database
- Redirecting to old domain after migration website
- Update from 4.5 to 4.6 failing
- Does WordPress $wpdb functions wait when table is locked?
- How Can I Put Meta_Compare in the Database-Query?
- WordPress running SQL query to update database from form
- $wpdb->insert Giving duplicates
- WPDB SQL query with prepare() returning variable, not db value
- $wpdb error (Call to a member function insert() on a non-object)
- Have working sql query… trying to adjust it to use $wpdb
- Is it important to have integers inserted using %d rather than %s?
- Problem in using wpdb
- $wpdb->query can’t insert data
- Custom WordPress Table wpdb
- Best practice to limit results in get_row()?
- CreateOrUpdate in WordPress
- Problem migrating to localhost
- Clear Terms from Taxonomy for Specific Post IDs?
- Trouble running $wpdb->query() with last_insert_id
- How to get a list of WordPress default database tables?
- Using $wpdb | checking entered email against existing emails in db
- Optimizing function that automatically creates internal links based on post title string
- How to connect and insert data in database of wordpress?
- Get results from wordpress data custom table
- Outputting query results
- add_post_meta — not working
- Select From wpdb – Author/User Directory page
- Creating and Using Tables in the WordPress Database
- $wpdb->insert not inserting all rows
- How to escape percentage sign(%) in sql query with $wpdb->prepare?
- wpdb Insert unknown post data dynamic foreach loop
- SELECT rows between two datetimes when the range is dynamic [closed]
- How to insert a value to decimal type field using wpdb->prepare?
- Insert NULL value using prepare()
- Help with $wpdb on custom code
- “BS_” rows in postmeta table
- $wpdb->insert duplicating rows
- How to rerieve comments and their replies from DB
- Can I use a wpdb object to connect to a non-WordPress Oracle database [duplicate]
- Where is the HTML-handler part in the wpdb class?
- WordPress Database Query works in phpMyAdmin but not in the code
- How to add download link from database on custom page template?
- How can I get $wpdb to show MySQL warnings?
- Correct and secure way to access a custom SQL database in a custom PHP template file
- About wp database hooks (error establishing connection)
- Using $wpdb to insert data into a table
- How To connect to the same WordPress database with different database user
- Should I Use only wpdb Class to Write Custom Queries?
- Call to a member function insert()
- I have include wp-config, should I add global $wpdb also?
- Processing a lot of $wpdb isn’t insert all the data
- Can’t Install WordPress (local) Failed to open file wp-includes/wp-db.php
- using same mysql user with many databases
- How can I retrieve data from alternate database using wpdb class?
- Creating Tables in WordPress Database
- How to use remote db tables in current config? [duplicate]
- $wpdb->prepare returns empty array
- How to delete field using WPDB?
- Uploading to WordPress Database