On this page
wpdb::update( string $table, array $data, array $where, array|string $format = null, array|string $where_format = null ): int|false
Updates a row in the table.
Description
Examples:
wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
See also
- wpdb::prepare()
- wpdb::$field_types
- wp_set_wpdb_vars()
Parameters
$tablestring Required-
Table name.
$dataarray Required-
Data to update (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. $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. $formatarray|string Optional-
An array of formats to be mapped to each of the values 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 $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 $where will be treated as strings.Default:
null
Return
int|false The number of rows updated, or false on error.
Source
File: wp-includes/class-wpdb.php. View all references
public function update( $table, $data, $where, $format = null, $where_format = null ) {
if ( ! is_array( $data ) || ! is_array( $where ) ) {
return false;
}
$data = $this->process_fields( $table, $data, $format );
if ( false === $data ) {
return false;
}
$where = $this->process_fields( $table, $where, $where_format );
if ( false === $where ) {
return false;
}
$fields = array();
$conditions = array();
$values = array();
foreach ( $data as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$fields[] = "`$field` = NULL";
continue;
}
$fields[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
}
foreach ( $where as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$conditions[] = "`$field` IS NULL";
continue;
}
$conditions[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
}
$fields = implode( ', ', $fields );
$conditions = implode( ' AND ', $conditions );
$sql = "UPDATE `$table` SET $fields 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_update_site() wp-includes/ms-site.php | Updates a site in the database. |
| wp_comments_personal_data_eraser() wp-includes/comment.php | Erases personal data associated with an email address from the comments table. |
| WP_Customize_Manager::trash_changeset_post() wp-includes/class-wp-customize-manager.php | Trashes or deletes a changeset post. |
| _wp_keep_alive_customize_changeset_dependent_auto_drafts() wp-includes/theme.php | Makes sure that auto-draft posts get their post_date bumped or status changed to draft to prevent premature garbage-collection. |
| WP_Customize_Manager::_publish_changeset_values() wp-includes/class-wp-customize-manager.php | Publishes the values of a changeset. |
| wp_add_trashed_suffix_to_post_name_for_post() wp-includes/post.php | Adds a trashed suffix for a given post. |
| update_network_option() wp-includes/option.php | Updates the value of a network option that was already added. |
| update_user_status() wp-includes/ms-deprecated.php | Update the status of a user in the database. |
| 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_insert_link() wp-admin/includes/bookmark.php | Inserts a link into the database, or updates an existing link. |
| WP_Posts_List_Table::_display_rows_hierarchical() wp-admin/includes/class-wp-posts-list-table.php | |
| wp_set_password() wp-includes/pluggable.php | Updates the user’s password with a new encrypted one. |
| update_usermeta() wp-includes/deprecated.php | Update metadata of user. |
| _update_post_term_count() wp-includes/taxonomy.php | Updates term count based on object types of the current taxonomy. |
| _update_generic_term_count() wp-includes/taxonomy.php | Updates term count based on number of objects. |
| wp_update_term() wp-includes/taxonomy.php | Updates term based on arguments provided. |
| 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. |
| update_option() wp-includes/option.php | Updates the value of an option that was already added. |
| wp_insert_user() wp-includes/user.php | Inserts a user into the database. |
| _transition_post_status() wp-includes/post.php | Hook for managing future post transitions to published. |
| wp_publish_post() wp-includes/post.php | Publishes a post by transitioning the post status. |
| add_ping() wp-includes/post.php | Adds a URL to those already pinged. |
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| wp_delete_post() wp-includes/post.php | Trashes or deletes a post or page. |
| wp_trash_post_comments() wp-includes/post.php | Moves comments for a post to the Trash. |
| set_post_type() wp-includes/post.php | Updates the post type for the post ID. |
| _wp_upgrade_revisions_of_post() wp-includes/revision.php | Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1. |
| wpmu_activate_signup() wp-includes/ms-functions.php | Activates a signup. |
| wp_xmlrpc_server::attach_uploads() wp-includes/class-wp-xmlrpc-server.php | Attach upload to a post. |
| wp_set_comment_status() wp-includes/comment.php | Sets the status of a comment. |
| wp_update_comment() wp-includes/comment.php | Updates an existing comment in the database. |
| wp_update_comment_count_now() wp-includes/comment.php | Updates the comment count for the post. |
| do_trackbacks() wp-includes/comment.php | Performs trackbacks. |
| wp_delete_comment() wp-includes/comment.php | Trashes or deletes a comment. |
| update_metadata_by_mid() wp-includes/meta.php | Updates metadata by meta ID. |
| update_metadata() wp-includes/meta.php | Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/update