Discover in this tutorial how to access values from the WordPress database and save them in a variable.
In most cases, when you want to access a data from the database of your WordPress installation you can do it through dedicated functions of the CMS itself or through the plugin that stores such data.
But sometimes you need to access tables that are not covered by those functions and you have to make a custom SQL query.
Luckily, WordPress also has a PHP class that can help you in this process.
Here you can see how.
Snippet to access a table value in the WordPress database
You must use the wpdb class that contains a series of functions to interact with the database (DB).
Thanks to it you can get values from both WordPress default tables and those created by themes and plugins.
You will see it more clearly with a couple of examples:
A – Accessing a WordPress table
In this example you access the users table, one of the 12 that WordPress creates when it is installed.
To see this and another 1097 code snippets of this website, login or subscribe here.
For this you use the wpdb class and the following variables:
- table prefix > ‘wp_’ (may vary depending on the installation)
- user_ID > takes the ID of the logged in user using the
get_current_user_id()
function; user_registered
(column where value is found)users
(DB table name without the prefix).
Therefore, the variable $user_registration_date
will contain the registration date of the user who is logged in.
By changing the table and the column you access you can get the value you need in each case.
Once you know how to access a data from the DB and save it in a variable you can use it in a function to perform further calculations or display it directly where you want.
Note: this is an illustrative example for you to understand how to use the wpdb class, since for this particular case it would be more effective to use the
get_users
function.
B – Access a custom table from a plugin
In this case you are going to see what code you need to access the activated_date
table that Restrict Content Pro plugin creates when the first payment is made and the membership is activated.
To see this and another 1097 code snippets of this website, login or subscribe here.
In this case you use the wpdb class and the following variables:
- table prefix -> ‘wp_’
- user_ID > takes the ID of the logged in user using the
get_current_user_id()
function; activated_date
(column where the value is found)rcp_memberships
(DB table name without the prefix).
Therefore, the variable $rcp_activated_date
will contain the date on which the first payment was made and, therefore, on which the RCP membership was activated for that logged-in user.
Now that you know how to get these values you are probably wondering how you can take advantage of this data, aren’t you?
Don’t worry, because in the next tutorial you will see a case study on how to use this data from Restrict Content Pro. 😉
Conclusions
So you want to access a WordPress data table value by doing a SQL query? Now you know how to do it using the WordPress wpdb class.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂