Updating and deleting records

These operations works similar to insert, by using the update and delete functions of the wpdb class. Let's take a look at the syntaxes:

$wpdb->update(  'table',  array( 'column1' => 'value1' ),  array( 'ID' => 1 ),  array( '%s' ),  array( '%d' ));

In this method, clause conditions as an array for the third parameter and format of the columns in where clause as fifth parameter. Other parameters are exactly the same as the insert function:

$wpdb->delete( 'table', array( 'ID' => 1 ), array( '%d' ) );

In this method, we only have to pass the delete conditions as an array and respective formats of the columns. The preceding query will delete the record with an ID equal to 1.

Get WordPress Development Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.