Is there any way to accomplish the following in WordPress with $wpdb->insert
or
$wpdb->query($wpdb->prepare))
:
INSERT into TABLE (column1, column2, column3)
VALUES
('value1', 'value2', 'value3'),
('otherval1', 'otherval2', 'otherval3'),
('anotherval1', 'anotherval2', 'anotherval3')
…etc
OK, I figured it out!
Setup arrays for Actual Values, and Placeholders
the initial Query:
Then loop through the the values you’re looking to add, and insert them in the appropriate arrays:
Then add these bits to the initial query:
You can also use this way to build the query:
I have came across with this problem and decided to build a more improved function by using accepted answer too:
Source: https://github.com/mirzazeyrek/wp-multiple-insert
In addition to inserting multiple rows using $wpdb, if you ever need to update existing rows following snippet should be helpful.
This is updated snippet of what @philipp provided above.
This is a bit late, but you could also do it like this.
not very nice, but if you know what you are doing: