Just looking for a bit of advice on how to handle this:

In the past (many moons ago) when I did the occasional php/mysql work, it was relatively easy to pull/display data from a table using the table header in the "WHERE" query. (i.e. SELECT * FROM 'table' WHERE 'column-name' = 'query'). The tables would have columns for each piece of information such as email, phone, address, etc.

I am currently working on a project using a table that was installed into the database by a plugin where I want to use some of that data in a different area of the site not related to the plugin.

Instead of a column for each type of information as above, this table has

  • a user_id column which is linked to the users wordpress account
  • user_id a meta_key column. This column has entries such as address, phone number, etc. (these would be the column names in my above example)
  • a meta_value column. This column holds the data for each type of meta_key (the actual address, phone number, etc.)

What I am looking to do is get the value for each item for a particular user_id and pre-fill a form that is submitted.

So their User ID, Name and email are pulled from the users table of the WP database when they are logged in using the wp_get_current_user() function. Once I have the User ID, I then want to display that user's address, phone number etc from the other table in my form but I'm stuck on the sql query

Something like:

global $wpdb;    
$current_user = wp_get_current_user();
$uid = $current_user->ID;

$table = $wpdb_>prefix . 'plugin-table';

I then want to select the user's address and phone number etc and display them as individual pieces of info, so:


so that I can use them where needed

What would be the correct way to query the database to get the info I'm looking for?

Hope that all made sense...

  • anybody…? Do I need to explain it better? Apr 28, 2020 at 5:10

3 Answers 3


So, the following works:

$result = $wpdb->get_results ( "SELECT * FROM $studentTable WHERE `user_id` = $uid AND `um_key` LIKE 'student_number' ");

foreach($result as $studentNumber){
    echo $studentNumber->um_value.'<br/>';

though I feel that 'foreach' is not really the correct syntax/terminology to be using?

global $wpdb;

$table_name = $wpdb->prefix . 'your_table_name';

$results = $wpdb->get_results(

    "SELECT * FROM $table_name"


foreach($results as $row)

    echo $row->id;


OK, so maybe the above wasn't worded very well?

I have since made some progress as follows:

global $wpdb;    
$current_user = wp_get_current_user();
$uid = $current_user->ID;
$fname = $current_user->first_name . " " . $current_user->last_name;
$email = $current_user->user_email;

$studentTable = $wpdb->prefix.'um_metadata';

$result = $wpdb->get_results ( "SELECT * FROM $studentTable WHERE `user_id` = $uid AND 'um_key' = 'student_number' ");

foreach($result as $studentNumber){
    echo $studentNumber->um_value;

Quick context:

  • um_key column is the type of data (phone, address, etc.)
  • um_value is the column with the information

If I remove AND 'um_key' = 'student_number' from the SQL query, the result will spit out all the rows that have the correct user_id, but I am only looking to output the specific row that has the um_key 'student_number'

Any help appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.