Title: wpdb::_insert_replace_helper
Published: April 25, 2014
Last modified: August 20, 2026

---

# wpdb::_insert_replace_helper( string $table, array $data, string[]|string $format = null, string $type = ‘INSERT’ ): int|false

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#changelog)

Helper function for insert and replace.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#description)󠁿

Runs an insert or replace query based on `$type` argument.

### 󠀁[See also](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#see-also)󠁿

 * [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)
 * wpdb::$field_types
 * [wp_set_wpdb_vars()](https://developer.wordpress.org/reference/functions/wp_set_wpdb_vars/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#parameters)󠁿

 `$table`stringrequired

Table name.

`$data`arrayrequired

Data to insert (in column => value pairs).
 Both `$data` columns and `$data` values
should be “raw” (neither should be SQL escaped). Sending a null value will cause
the column to be set to NULL – the corresponding format is ignored in this case.

`$format`string[]|stringoptional

An array of formats to be mapped to each of the value in `$data`.
 If string, that
format will be used for all of the values in `$data`. A format is one of `'%d'`,`'%
f'`, `'%s'` (integer, float, string). If omitted, all values in `$data` will be 
treated as strings unless otherwise specified in wpdb::$field_types.

Default:`null`

`$type`stringoptional

Type of operation. Either `'INSERT'` or `'REPLACE'`.
 Default `'INSERT'`.

Default:`'INSERT'`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#return)󠁿

 int|false The number of rows affected, or false on error.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#source)󠁿

    ```php
    public function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    	$this->insert_id = 0;

    	if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) {
    		return false;
    	}

    	$data = $this->process_fields( $table, $data, $format );
    	if ( false === $data ) {
    		return false;
    	}

    	$formats = array();
    	$values  = array();
    	foreach ( $data as $value ) {
    		if ( is_null( $value['value'] ) ) {
    			$formats[] = 'NULL';
    			continue;
    		}

    		$formats[] = $value['format'];
    		$values[]  = $value['value'];
    	}

    	$fields  = '`' . implode( '`, `', array_keys( $data ) ) . '`';
    	$formats = implode( ', ', $formats );

    	$sql = "$type INTO `$table` ($fields) VALUES ($formats)";

    	$this->check_current_query = false;
    	return $this->query( $this->prepare( $sql, $values ) );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wpdb.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wpdb.php#L2586)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wpdb.php#L2586-L2617)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::process_fields()](https://developer.wordpress.org/reference/classes/wpdb/process_fields/)`wp-includes/class-wpdb.php` |

Processes arrays of field/value pairs and field formats.

  | 
| [wpdb::query()](https://developer.wordpress.org/reference/classes/wpdb/query/)`wp-includes/class-wpdb.php` |

Performs a database query, using current database connection.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#)

| Used by | Description | 
| [wpdb::insert()](https://developer.wordpress.org/reference/classes/wpdb/insert/)`wp-includes/class-wpdb.php` |

Inserts a row into the table.

  | 
| [wpdb::replace()](https://developer.wordpress.org/reference/classes/wpdb/replace/)`wp-includes/class-wpdb.php` |

Replaces a row in the table or inserts it if it does not exist, based on a PRIMARY KEY or a UNIQUE index.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wpdb/_insert_replace_helper/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwpdb%2F_insert_replace_helper%2F)
before being able to contribute a note or feedback.