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
$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 ) );
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
$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 ) );
Recommanded Articles
- Generate Dummy Posts in WordPress
- How to add captcha in Contact Form 7
- how to create a cron job in wordpress
- How to get form data in Contact Form 7
- Insert Update Delete Select query in WordPress
- How to increase website speed
- Create plugin in WordPress in which create table in DB on activate
- Create new tab under my account WooCommerce frontend
- Custom image sizes in WordPress
- Get posts by meta key and meta value in WordPress
Latest Comments
Buzzy
21 Dec 2020