Inserting Data into the WordPress Database:
$data = array('column1' => 'value1', 'column2' => 'value2'); $wpdb->insert('my_table', $data);
Updating Data in the WordPress Database:
$data = array('column1' => 'new_value1', 'column2' => 'new_value2'); $where = array('id' => 1); $wpdb->update('my_table', $data, $where);
Querying the Database with WP_Query:
$args = array('post_type' => 'post', 'posts_per_page' => 5); $query = new WP_Query($args); while ($query->have_posts()) { $query->the_post(); // Loop content here }
Using $wpdb to Perform Custom Queries:
$results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page'");
Using wpdb to Create Tables:
function create_custom_table() { global $wpdb; $table_name = $wpdb->prefix . 'custom_table'; $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, name varchar(50), PRIMARY KEY (id) )"; $wpdb->query($sql); }
Updating Post Meta Data:
$post_id = 1; update_post_meta($post_id, 'custom_key', 'new_value');
Querying Database with $wpdb:
global $wpdb; $results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page'");
Resetting User Password Programmatically:
$user_id = 1;
wp_set_password('new_password', $user_id);