plugin development - How to get inserted row IDs for bulkbatch insert with wpdb query?

admin2025-01-07  5

I'm writing a plugin where I need to save data in custom tables, function hits an API and then saves data for which I need bulk insert, but as wpdb doesn't support it out of the box I made a function for it using prepare and query.

But query doesn't return inserted row IDs. How can I get them? Any other way or anything besides using wpdb?

I'm writing a plugin where I need to save data in custom tables, function hits an API and then saves data for which I need bulk insert, but as wpdb doesn't support it out of the box I made a function for it using prepare and query.

But query doesn't return inserted row IDs. How can I get them? Any other way or anything besides using wpdb?

Share Improve this question asked Jun 15, 2020 at 7:33 ateebahmedateebahmed 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As described here:

If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.

So if you need every inserted ID you should use single $wpdb->insert commands, then you have the ID available for each with $wpdb->insert_id. Docs here: https://codex.wordpress.org/Function_Reference/wpdb_Class#INSERT_row

If you insert many rows with a single query you can't get all the inserted ID's, unless you e.g. wanted to re-query for them if you are sure you can identify those rows uniquely.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257559a448.html

最新回复(0)