HowTo – Accessing The Database Within WordPress For Functions & Plugins

Filed under: Wordpress Tips

wordpress-logoThere are times when you need to look something up or set a field in your database from within your Theme Files, functions.php file or your plugin.

Although you can access the database directly with SQL commands you can sometimes run into problems. In addition to that if you use the WordPress API wpdb class then you can help reduce problems of injections and information corruption.

The wpdb class is pretty easy to work with and it will give you the ability to retrieve, set and update data in any part of the WordPress Database.

This can be useful when you need to perform complex lookups that may include information stored by wordpress, your plugin or another authors plugin.

The wpdb class can only connect to your database if you need to connect to an outside database then you will need to instantiate your own object from the wpdb class with the appropriate connection details.

Making Calls

In its simplest form you can simply query the database

<?php $wpdb->query('query'); ?>

For more specific access you can use

<?php $wpdb->get_var('query',column_offset,row_offset); ?>

(string) The query you wish to run. Setting this parameter to null will return the specified variable from the cached results of the previous query.
(integer) The desired column (0 being the first). Defaults to 0.
(integer) The desired row (0 being the first). Defaults to 0.

To select a specific Row you would use

<?php $wpdb->get_row('query', output_type, row_offset); ?>

To select a specific Column you would use

<?php $wpdb->get_col('query',column_offset); ?>

Error Reporting

While working with your code you can set error reporting and echo the information returned.

<?php $wpdb->show_errors(); ?>
<?php $wpdb
->hide_errors(); ?>

<?php $wpdb->print_error(); ?>

Making Use Of The Class

There are many instances where you can make use of this class but for most end users the wpdb class will be used for sorting your posts.

Remember you can retrieve any fields from the WordPress database so your options are only limited to your creativity.

You could create a query that would return posts made between 11am and 1pm and categorize them as lunch time posts by placing a query in a category-123.php theme file.

You could also include a function in your functions.php file that would look for the presence of required plugins and their versions to make sure your theme is properly setup and then return that information to your end user by echoing the info and warning them that the install requires more attention before your features will work correctly. This would require careful monitoring of outside resources but could be useful.

How you make use of the data within your database is up to your own creativity.

The wpdb class will help you make your connections quickly and manage your data as needed.

Final Note

Although this interface does not change dramatically between versions  you should keep upto date on the specifics of the class by reading the codex pages on

Remember while testing you should always work with a non live site and if you are using features that could cause problems inform your users before they install your themes or plugins.

For a full and up to date reference you should check the Codex Page.

<?php $wpdb->get_col('query',column_offset); ?>