WordPress has a built-in Data object class for dealing with SQL query of Insert Update Select and Delete database queries. It’s called wpdb as well as this is a get in the path of includes/wp-db.php file. When you are running queries you required always use this added class to execute them. To use $wpdb class you first required to define $wpdb as a global main WordPress $wpdb variable before you can use it. Just place $wpdb added line of wordpress CMS PHP Source code before every global variable declare as $wpdb method:
Before we get into the nitty-gritty of $wpdb, we should mention that messing with your database unless you’re absolutely confident you know what you’re doing is a bad idea. If you think that your website will require you to do this often, and you aren’t well versed with development, it’s probably a good idea to look into WordPress maintenance plans that offer continuous support.
Functions for running database queries
The $wpdb->query method is used mainly for SELECT as well as DELETE statements but can actually be used for any sql statement.
$wpdb->query – any query method
$wpdb->query($wpdb->prepare ("DELETE FROM $wpdb->user_define_tbl WHERE ID = 1" ));
As you can see, we use the wp->user_define_tbl to reference a custom Database table in our query.
$wpdb->get_var – return single variable
The get_var() method is used for retrieving a single variable from the database
$count_posts = $wpdb->get_var( $wpdb->prepare(" SELECT COUNT(*) FROM $wpdb->wp_posts" )); echo "$count_posts";
$wpdb->get_row – return single row
To retrieve an entire row You would like to use the get_row() method. By default the row is returned as an object, but you can add the PRODUCT_ARRAY or PRODUCT_ARR_ALL parameter at the end of your method. The PRODUCT_ARRAY parameter will return an associative array and the PRODUCT_ARR_ALL parameter will return a numerically index array.
$wpdb->get_row( $wpdb->prepare( "SELECT * from $wpdb->posts WHERE ID=1"), PRODUCT_ARRAY );
$wpdb->get_results – return multiple rows
To retrieve multiple rows from a Database table You would like to use the $wpdb->get_results() method.
$wpdb->get_results( $wpdb->get_results( $wpdb->prepare( " SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' "));
This wordpress select query in wordpress will fetch all the published all the available group of the posts as Data an object.
$wpdb->insert – Insert query
insert query in wordpress for custom table The are specific wordpress wpdb methods for an insert or update sql. The basic usage of the insert() method
$wpdb->num_rows – number of rows returned
We use the num_rows() methods to return the number of rows returned by a sql query.
$wpdb->print_error() – display sql errors
When dealing with usage functions it can be hard to debug what’s wrong with your sql. The print_error() method will display the errors of your sql to the page
$wpdb->show_errors(); $wpdb->get_row( $wpdb->prepare(" SELECT * FROM $wpdb->posts WHERE ID = 3000"); // unexisting ID $wpdb->print_error();
The show_errors() method must be called before executing a sql query. The print_error() method directly after running the query.
$wpdb->last_query – print last query executed
The last_query() method will print the last sql query executed. This can be useful to test why a query isn’t working as expected.
var_dump( $wpdb->last_query );
update query in sql
$wpdb->update – update query : update query in wordpress with where clause
We do a update query in a similar fashion. I first of all put our Database table, and then data pass our Column values in an array and then put our where clause variable.
$wpdb->update( $table, $data, $where );
The where variable is an array of Column names and Database table Column values for the where clause. In this example We shall use a unique ID in our mysql where clause.
$wpdb->update( $wpdb->posts, array( 'post_title' => "Welcome to papega 2020", 'post_content' => "free download bolly4u"), array( "ID", 1));
Both the insert() and update() do not required to use the prepare() method because both these methods use the prepare method internally.
insert query in sql with PHP
Question: how to insert data in mysql using wordpress? also WordPress CMS System allow the $wpdb class of functions for all the data interacting with the PHP MySQL database.
Use $wpdb->insert(). – wpdb->insert id
$wpdb->insert('wp_submitted_form', array( 'name' => 'Pakainfo', 'email' => '[email protected]', 'phone' => '989822566666', // ... and so on ));
select query in wordpress
For example to PHP MySQL SELECT query to get information from a custom Database table called “postmeta”, you can do the bellow Query.
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'pakainfo-post-jdkgroup' AND meta_value = '". $data_frm ."')"); print_r($post_id);
insert query in wordpress
An example of an insert sql query:
$shop_name = "Pakainfo, com"; $store_address = "Kothariya ring road chokdi rajkot"; $wpdb->insert( $wpdb->user_define_tbl, array("shop_name" => $shop_name, "store_address" => $store_address ));
You first required to set your Database table and then pass your field values in an array. No required to use the prepare method here. We shall learn why in the update MySQL statement query.
Web Programming Tutorials Example with Demo
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about select Insert Update and Delete query in wordpress.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.