Want to insert query in WordPress database? Below is an example of how to insert in database in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $wpdb->insert('wp_email_subscription', array( 'name' => 'name', ), array( '%s', '%s' ) ); ?> |
Below example is to select data from database in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php // set the meta_key to the appropriate custom field meta key $meta_key = 'miles'; $allmiles = $wpdb->get_var( $wpdb->prepare( " SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s ", $meta_key ) ); echo "<p>Total miles is {$allmiles}</p>"; ?> |
Another example of retrieve and display the number of users in your WordPress site:
1 2 3 4 | <?php $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" ); echo "<p>User count is {$user_count}</p>"; ?> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.