| | 1256 | |
| | 1257 | /** |
| | 1258 | * Delete a row in the table |
| | 1259 | * |
| | 1260 | * <code> |
| | 1261 | * wpdb::delete( 'table', array( 'ID' => 1 ) ) |
| | 1262 | * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
| | 1263 | * </code> |
| | 1264 | * |
| | 1265 | * @since 2.5.0 |
| | 1266 | * @see wpdb::prepare() |
| | 1267 | * @see wpdb::$field_types |
| | 1268 | * @see wp_set_wpdb_vars() |
| | 1269 | * |
| | 1270 | * @param string $table table name |
| | 1271 | * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
| | 1272 | * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings. |
| | 1273 | * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. |
| | 1274 | * @return int|false The number of rows updated, or false on error. |
| | 1275 | */ |
| | 1276 | function delete( $table, $where, $where_format = null) { |
| | 1277 | if ( ! is_array( $where ) ) |
| | 1278 | return false; |
| | 1279 | |
| | 1280 | $bits = $wheres = array(); |
| | 1282 | $where_formats = $where_format = (array) $where_format; |
| | 1283 | foreach ( (array) array_keys( $where ) as $field ) { |
| | 1284 | if ( !empty( $where_format ) ) |
| | 1285 | $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; |
| | 1286 | elseif ( isset( $this->field_types[$field] ) ) |
| | 1287 | $form = $this->field_types[$field]; |
| | 1288 | else |
| | 1289 | $form = '%s'; |
| | 1290 | $wheres[] = "`$field` = {$form}"; |
| | 1291 | } |
| | 1292 | |
| | 1293 | $sql = "DELETE FROM `$table` WHERE " . implode( ' AND ', $wheres ); |
| | 1294 | return $this->query( $this->prepare( $sql, $where ) ); |
| | 1295 | } |
| | 1296 | |
| | 1297 | |