Insert Update Delete Select query in WordPress

Insert Update Delete Select query in WordPress

Here I will explain how you can insert, update, delete, select data in wordpress  using custom queries. WordPress provides a global object variable for do this functionality `$wpdb`, which is an instantiation of the wpdb class defined in /wp-includes/wp-db.php. By default, $wpdb is instantiated to talk to the WordPress database. To access $wpdb in your WordPress PHP code, declare $wpdb as a global variable using the global keyword, or use the superglobal $GLOBALS in the following way.

global $wpdb;

Also Read: Create Custom Table in WordPress

I will use a custom table named `custom_table` in which columns are title,content,inserted_time, updated_time. Use below queries to insert, update, delete and select data in wordpress from table `custom_table`.

Insert Query in wordpress

$wpdb->insert( 
    $wpdb->prefix.'custom_table', 
    array( 
        'title'      => $_POST['type'],
        'content'      => $_POST['image'],
        'inserted_time'     => $_POST['image_award'],
        'updated_time'      => $_POST['icon']
    )
);

Select Query in wordpress

Also Read: Capture page screenshot by URL PHP script
$wpdb->get_results("SELECT * FROM ".$wpdb->prefix."custom_table where id=1");

Update Query in wordpress

$wpdb->query("UPDATE ".$wpdb->prefix."custom_table SET title = 'mytitle' WHERE id = ".$rows1[0]->id);

Delete Query in wordpress

$wpdb->delete( $wpdb->prefix.'custom_table', array( 'id' => 1 ) );

 

Also Read: How to parse an URL in PHP

Here I will explain how you can insert, update, delete, select data in wordpress  using custom queries. WordPress provides a global object variable for do this functionality `$wpdb`, which is an instantiation of the wpdb class defined in /wp-includes/wp-db.php. By default, $wpdb is instantiated to talk to the WordPress database. To access $wpdb in your WordPress PHP code, declare $wpdb as a global variable using the global keyword, or use the superglobal $GLOBALS in the following way.

global $wpdb;

Also Read: Create Custom Table in WordPress

I will use a custom table named `custom_table` in which columns are title,content,inserted_time, updated_time. Use below queries to insert, update, delete and select data in wordpress from table `custom_table`.

Insert Query in wordpress

$wpdb->insert( 
    $wpdb->prefix.'custom_table', 
    array( 
        'title'      => $_POST['type'],
        'content'      => $_POST['image'],
        'inserted_time'     => $_POST['image_award'],
        'updated_time'      => $_POST['icon']
    )
);

Select Query in wordpress

Also Read: Capture page screenshot by URL PHP script
$wpdb->get_results("SELECT * FROM ".$wpdb->prefix."custom_table where id=1");

Update Query in wordpress

$wpdb->query("UPDATE ".$wpdb->prefix."custom_table SET title = 'mytitle' WHERE id = ".$rows1[0]->id);

Delete Query in wordpress

$wpdb->delete( $wpdb->prefix.'custom_table', array( 'id' => 1 ) );

 

Also Read: How to parse an URL in PHP

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Buzzy
Buzzy
21 Dec 2020

My update query was not working, i used your`s. IT WORKS NOW> THANKS

Add your comment

Close