As we are familiar with the functions that

As we are familiar with the functions that WordPress uses to interact with the database. Some common functions are WP_Query, wp_insert_post, get_posts, update_post_meta which can be used to get the job done.But sometimes we required accessing the database directly, especially when we are going to fetch data from a custom table. Wordpress has also provided a valid way to make it easy for the users by introducing a $wpdb class.Working with $wpdb ClassWorking with $wpdb class is similar to working with MySql and other similar languages.We need to keep only few function names in mind. We can understand the basic usage through an example, so let’s query our database for the IDs and titles of the five most recent posts, ordered by comment count (in descending order).get_results(“SELECT ID, post_title FROM $wpdb->posts WHERE post_status = ‘publish’   AND post_type=’post’ ORDER BY comment_count DESC LIMIT 0,5”)?>Fetching Results from the Database$wpdb has four helper functions to retrieve data from the query, you can use one of four functions to structure the data.GET_RESULTS() It is most commonly used function, it creates a two dimensions array of the data that contains separate objects for each row.GET_ROW() When we need to fetch only one row from the database. It gives one-dimensional output.GET_COL() It is similar to get_row() functions instead row it gives a single column which is helpful when we need to get only post ID.GET_VAR() Sometimes we need to get only one value from the database, then we can use get_var() to retrieve a value. The data type of value will be the same as its type in the DB.Inserting into the Database$wpdb->insert( $table, $data, $format);The above method takes three arguments. The first parameter is the name of the table. The second parameter is an array contains a combination of key-value pairs. The last one is optional which specifies the data types of the values.Updating data into the Database$wpdb->update( $table, $data, $where, $format = null, $where_format = null );The $table, $data and the $where parameter, we can specify the conditions of the update. You can read more about these at here( The wpdb() class should not be called directly. Always use the global $wpdb object instead!


I'm Harold!

Would you like to get a custom essay? How about receiving a customized one?

Check it out