SELECT

global $wpdb;
$result = $wpdb->get_results (

SELECT *
FROM wp_posts
WHERE post_title = ‘Hello world!’

);

print_r($result);

Description:

WordPress provides a class wpdb which is present in the file – wp-includes\wp-db.php. This class abstracts the database functions for WordPress and most WordPress functions directly or indirectly use this class. This class is based on the ezSQL class. You can create an object of this class to perform database operations but WordPress creates an object of this class during the load of WordPress. This query give the output of Hello world! which can be found from the table wp_posts.

Another Example of SELECT Query

global $wpdb;
$sql=”SELECT * from wp_posts where post_type=’post'”;
$results = $wpdb->get_results($sql) or die(mysql_error());
if (count($results) > 0) {
$display_row = null;
foreach ($results as $res) {
print_r($result);

Description

In this example we used a foreach loop. It can check the post_type from the table wp_posts, and show the result which can be true.

Update

$update_post=array(‘ID’=>’1’, ‘post_title’=>’Andriod Bubbes’);
$result=wp_update_post($update_post);
print_r($result);

Description

wp_update allow you to easily update your wordpress theme. It will update a single id at a time. In the above example we can update the post_title, which have ID=1. It update our table according to our ID, which we given.

DELETE

lobal $wpdb;
$result= $wpdb->delete( ‘wp_posts’, array( ‘ID’ => 1 ) );
print_r($result);

Description

This query will delete the post which have id=1. It will delete only post which we define by id.

Advertisements