wpdb::delete PHP Method

delete() public method

wpdb::delete( 'table', array( 'ID' => 1 ) ) wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
See also: wpdb::prepare()
See also: wpdb::$field_types
See also: wp_set_wpdb_vars()
Since: 3.4.0
public delete ( string $table, array $where, array | string $where_format = null ) : integer | false
$table string table name
$where array 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".
$where_format array | 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 unless otherwise specified in wpdb::$field_types.
return integer | false The number of rows updated, or false on error.
    public function delete($table, $where, $where_format = null)
    {
        if (!is_array($where)) {
            return false;
        }
        $wheres = [];
        $where_formats = $where_format = (array) $where_format;
        foreach (array_keys($where) as $field) {
            if (!empty($where_format)) {
                $form = ($form = array_shift($where_formats)) ? $form : $where_format[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $wheres[] = "{$field} = {$form}";
        }
        $sql = "DELETE FROM {$table} WHERE " . implode(' AND ', $wheres);
        return $this->query($this->prepare($sql, $where));
    }

Usage Example

Example #1
0
 /**
  * Delete a specfic ruleset from database.
  *
  * @param int $rulesetId Primary key of the ruleset.
  * @return bool True if ruleset was deleted or false otherwise.
  */
 public function deleteRuleset($rulesetId)
 {
     if (empty($rulesetId)) {
         return false;
     }
     $deleteResult = $this->wpdb->delete($this->wpdb->rulesets, array('id' => (int) $rulesetId));
     return $deleteResult === false ? false : true;
 }
All Usage Examples Of wpdb::delete