WordPress $wpdb Insert Multiple Records

Today, We want to share with you WordPress $wpdb Insert Multiple Records.In this post we will show you mysql insert or update if exists multiple rows, hear for database – wpdb->insert multiple record at once we will give you demo and example for implement.In this post, we will learn about How to insert or update multiple items in MySQL and WordPress with an example.

WordPress $wpdb Insert Multiple Records

There are the Following The simple About WordPress $wpdb Insert Multiple Records Full Information With Example and source code.

As I will cover this Post with live Working example to develop mysql on duplicate key update multiple keys, so the insert data in wordpress for this example is following below.

function ntpr_default_save_products($data){
  global $wpdb;
  $prefix = $wpdb->prefix.NTPR_DATA;

    $products = array();
      $products = $data['create'];

    $products_query = "INSERT INTO ".$prefix."products (status, name, user_id, topic, slug) VALUES ";

    foreach ( $products as $an_item ) {
      $products_query .= $wpdb->prepare(
        "(%d, %s, %d, %s, %s),",
        1, $an_item['name'], 1, $an_item['topic'], $an_item['slug']

    $products_query = rtrim( $products_query, ',' ) . ';';

    if($wpdb->query( $products_query ))
      return true;
      return false;

Yes, you can use the $wpdb->query() method to perform bulk inserts/updates in WordPress. Here’s an example of how to use it:

global $wpdb;

// Define your data to insert/update
$data = array(
  array('column1' => 'value1', 'column2' => 'value2', 'column3' => 'value3'),
  array('column1' => 'value4', 'column2' => 'value5', 'column3' => 'value6'),
  // add more rows as needed

// Construct the query
$query = "INSERT INTO table_name (column1, column2, column3) VALUES ";
foreach ($data as $row) {
  $query .= sprintf("('%s', '%s', '%s'),", $row['column1'], $row['column2'], $row['column3']);
$query = rtrim($query, ',');

// Execute the query

This example shows how to insert multiple rows into a table with the column names column1, column2, and column3. You can modify the code to fit your specific use case, such as updating existing rows instead of inserting new ones. However, please be careful when using this method as it does not provide any protection against SQL injection attacks. It’s important to sanitize and validate the data before inserting or updating it in the database.

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Word-Press $wpdb Insert Multiple Records.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment