wordpress update query – Update Statement Tutorial – Update Query Syntax & Examples

wordpress update query with insert and delete in mysql database in wordpress. Here We will explain how you can insert, update, delete, select data in wordpress using custom queries MysqL.

wordpress update query – wordpress query : insert, update and delete

First of all We write manually update, delete, insert and select query and run data with mysql_query function Like this bellow Examples:

WordPress update mysql table

$wpdb->query($wpdb->prepare("UPDATE $db_tbl_nm SET time='$date_time_current' WHERE userid=$userid"));

update query wordpress

global $wpdb;
$dbData = array();
$dbData['last_login_time'] = time();

$wpdb->update('db_tbl_nm', $dbData, array('user_id' => 1));

update wordpress query

$new_query= $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %d WHERE ID = %s", "pakainfo_v1", 546 ) );
var_dump($new_query);

How To Insert/Update Data Using $Wpdb

There Are main 2 Ways To get $Wpdb Object:

using $wpdb : wordpress update query – $wpdb as global as well as used it to run an SQL simple query statement that returns a PHP object.

	global $wpdb;
	$results = $wpdb->function

Using $GLOBALS : simple get $GLOBALS superglobal. Does not need global keyword.

	$results = $GLOBALS['wpdb']->function

How To Insert Record Using $Wpdb:

I can use insert method to insert row data into mysql database. Also Read : wordpress Insert query

	$wpdb->insert('db_tbl_nm', array('id'=>'1', 'name'=>'pakainfo'));

How To Update Record Using $Wpdb:

I can use update method to insert data into mysql database.

	$wpdb->update('db_tbl_nm', array('id'=>'1', 'name'=>'pakainfo', array('id' => '2')));

How To Delete Record Using $Wpdb

The $wpdb is having simple delete() method to delete the row from wordpress db.

wpdb::delete( ‘table’, array( ‘ID’ => 1 ), array( ‘%d’ ) )

How to use update and delete query in wordpress?

Select query

$prefix = $wpdb->prefix;
$postSql = "SELECT DISTINCT post_id
            FROM " . $prefix . "postmeta As meta
            Inner Join " . $prefix . "posts As post
            On post.ID = meta.post_id
            Where post_type = 'product' 
            And post_status = 'publish'
            And meta_key Like '%product_img%'";
$postQry = mysql_query($postSql);
while ($postRow = mysql_fetch_array($postQry)) {
     $post_id = $postRow['post_id'];
}

Insert Query

$all_website_data = "Insert Into " . $prefix . "postmeta(post_id,meta_key,meta_value) Value('$post_id','$meta_key','$data_serialize')";
        mysql_query($all_website_data);

Update Query:

$update_price = "Update " . $prefix . "postmeta
                         Set meta_key = 'website_pakainfoi_v1'
                         Where post_id = $supportMetaID
                         And meta_key Like '%new_demo_key%'";
 mysql_query($update_price);

Delete Query

mysql_query("Delete From " . $prefix . "postmeta Where meta_key IN ('website_img1','website_img2','website_img3')");

use wordpress queries like

$wpdb->get_results( "SELECT id, name FROM pakainfo_tbl_name" );
$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
);
$wpdb->update( 
    'table', 
    array( 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    ), 
    array( 'ID' => 1 )
);
$wpdb->delete( 'table', array( 'ID' => 1 ) );

WordPress update mysql table

Updating a MySQL table in WordPress can be done by using the $wpdb global object, which is a WordPress class that provides an easy-to-use database access API. Here’s an example code that demonstrates how to update a MySQL table in WordPress:

global $wpdb;

$table_name = $wpdb->prefix . 'my_table_name'; // Replace "my_table_name" with the name of your table.

// Update the table with new values.
$wpdb->update( 
    $table_name, 
    array( 
        'column1' => 'new_value1', 
        'column2' => 'new_value2' 
    ), 
    array( 'id' => 1 ), // Replace "id" with the name of the primary key column and "1" with the value you want to update.
    array( 
        '%s', // For string values.
        '%s' 
    ), 
    array( '%d' ) 
);

In this code, the $wpdb->prefix variable is used to get the WordPress database table prefix, which is used to construct the full table name. Replace my_table_name with the name of your table.

The update() method of the $wpdb object is used to update the table. The first argument is the table name, the second argument is an array of column names and new values, the third argument is an array of conditions for the update (in this case, the ID is 1), the fourth argument is an array of data types for the new values, and the fifth argument is an array of data types for the conditions.

Make sure to replace the column names, new values, and condition values with your own values.

Note: Updating the database directly through code can be dangerous, so make sure to take a backup of your database before making any updates. Also, use caution and make sure to sanitize any input values to prevent SQL injection attacks.

I hope you get an idea about wordpress update query.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment