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.

Free Live Chat for Any Issue

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 ) );

How To Insert/Update Data Using $Wpdb

There Are main 2 Ways To get $Wpdb Object:

Also Read This 👉   PHP GD Library Resize Images Manipulating

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')";

Update Query:

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

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" );
        'column1' => 'value1', 
        'column2' => 123 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    array( 'ID' => 1 )
$wpdb->delete( 'table', array( 'ID' => 1 ) );

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.