On this page
wpdb::delete( string $table, array $where, array|string $where_format = null ): int|false
Deletes a row in the table.
Description
Examples:
wpdb::delete( 'table', array( 'ID' => 1 ) )
wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
See also
- wpdb::prepare()
- wpdb::$field_types
- wp_set_wpdb_vars()
Parameters
$tablestring Required-
Table name.
$wherearray Required-
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".
Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case. $where_formatarray|string 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 $data will be treated as strings unless otherwise specified in wpdb::$field_types.Default:
null
Return
int|false The number of rows deleted, or false on error.
Source
File: wp-includes/class-wpdb.php. View all references
public function delete( $table, $where, $where_format = null ) {
if ( ! is_array( $where ) ) {
return false;
}
$where = $this->process_fields( $table, $where, $where_format );
if ( false === $where ) {
return false;
}
$conditions = array();
$values = array();
foreach ( $where as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$conditions[] = "`$field` IS NULL";
continue;
}
$conditions[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
}
$conditions = implode( ' AND ', $conditions );
$sql = "DELETE FROM `$table` WHERE $conditions";
$this->check_current_query = false;
return $this->query( $this->prepare( $sql, $values ) );
}
Related
Uses
| Uses | Description |
|---|---|
| wpdb::process_fields() wp-includes/class-wpdb.php | Processes arrays of field/value pairs and field formats. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| wp_delete_signup_on_user_delete() wp-includes/ms-functions.php | Deletes an associated signup entry when a user is deleted from the database. |
| wp_delete_site() wp-includes/ms-site.php | Deletes a site from the database. |
| delete_network_option() wp-includes/option.php | Removes a network option by name. |
| wpmu_delete_user() wp-admin/includes/ms.php | Delete a user from the network and remove from all sites. |
| wp_install_defaults() wp-admin/includes/upgrade.php | Creates the initial content for a newly-installed site. |
| wp_delete_user() wp-admin/includes/user.php | Remove user and optionally reassign posts and links to another user. |
| wp_delete_link() wp-admin/includes/bookmark.php | Deletes a specified link from the database. |
| wp_insert_term() wp-includes/taxonomy.php | Adds a new term to the database. |
| wp_delete_term() wp-includes/taxonomy.php | Removes a term from the database. |
| delete_option() wp-includes/option.php | Removes option by name. Prevents removal of protected WordPress options. |
| wp_delete_attachment() wp-includes/post.php | Trashes or deletes an attachment. |
| wp_delete_post() wp-includes/post.php | Trashes or deletes a post or page. |
| wpmu_validate_user_signup() wp-includes/ms-functions.php | Sanitizes and validates data required for a user sign-up. |
| wpmu_validate_blog_signup() wp-includes/ms-functions.php | Processes new site registrations. |
| wp_delete_comment() wp-includes/comment.php | Trashes or deletes a comment. |
| delete_metadata_by_mid() wp-includes/meta.php | Deletes metadata by meta ID. |
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/delete