Working with database in WordPress (we should globalize $wpdb variable):
<?php global $wpdb; ?>
Get results
<?php $results = $wpdb->get_results("select * from $wpdb->posts where post_status = 'draft' and post_author = 5"); foreach ($results as $result) { echo $result->post_title; } ?>
Get row
<?php $row = $wpdb->get_row("select * from $wpdb->links where link_id = 25"); echo $row->link_id; // prints "25" ?>
Insert row
<?php $wpdb->insert( $wpdb->posts, array( 'column1' => 'value1', // string 'column2' => 123, // decimal 'column3' => 12.5 // float ), array( '%s', '%d', '%f' ) // format (optional) (string type by default) ); $insert_id = $wpdb->insert_id; // the value of AUTO_INCREMENT column after insert ?>
Update row
<?php $wpdb->update( $wpdb->posts, array( 'column1' => 'value1', // string 'column2' => 22 // decimal ), array( 'ID' => 15 ), // where array( '%s', '%d' ), // format (optional) array( '%d' ) // where_format (optional) ); ?>
Run any query
<?php $wpdb->query( $wpdb->prepare( "delete from tablename where post_id=%d and meta_key=%s", $number, $string )); ?>
