I m creating a user based website in Wordpress, where each users can create an account and upload data of their records into the website. But I m having trouble thinking of how users can view a database table that presents relevant data to the user.

Its a golf website where the user can lodge in their exercises, what equipment they used and when they exercised.

I m trying to query from my database table for data specific to a currently login user. There is going to be a table named "exercise_record" and each WP user can view their own exercise record i.e.

global $wpdb;
$table_name = $wpdb ->prefix. 'exercise_record';

$current_user = wp_get_current_user(); // Getting current user logged in.
$user_id      = $current_user->ID;     // Get user ID to use in meta function.

$rows = $wpdb->get_results(
  FROM $table_name' 
  WHERE user =  %d", $user_id 

foreach  ($rows as $row) 
  $row->, 'exercise_record'

1 Answer 1



WordPress already have a table for users. Also, there is another table named usermeta that will allow you to save extra information for users.

There are some functions that allow developers to get information from user and usermeta tables without using custom database queries.

Solution: As mentioned in the question, users should see only their own data. So we can consider that the user is logged in to the site.

In order to get the current user, use the wp_get_current_user functions. The rest of the things will be done using user meta. Update a user meta based on data and it will be saved on the usermeta table. And get it whenever you want to show it.

$current_user = wp_get_current_user(); // Getting current user logged in.
$user_id      = $current_user->ID;     // Get user ID to use in meta function.

// Custom html markups

// Show user name using first name and last name.
echo 'User name: ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . '<br>';

// Show extra information.
echo get_user_meta( $user_id, 'prefix_user_exercise', true ) . '<br>';

And consider getting data for prefix_user_exercise in your frontend and update it using

$user_id = get_current_user_id(); // Get user id of logged in user (Bob).
update_user_meta( $user_id, 'prefix_user_exercise', '3/4 swing' );

In order to use a custom table to save additional data for a user, you will need the following code to get information.

global $wpdb;
$user_id = get_current_user_id();

$sql = "SELECT * FROM {$wpdb->prefix}my_table_name WHERE ID=%d";
$user_data = $wpdb->get_results( $wpdb->prepare( $sql, $user_id ) );

WPDB documentation

  • thanks for letting me know about wp_get_current_user function, that narrows it down. Instead of using get_user_meta to get information pertaining to a user, how should I go about querying a data from a custom table by current user login?
    – Yuan Lim
    Oct 28, 2019 at 2:08
  • It's depend on how is your custom table structure. Once you give custom table definition, anyone can provide answer. Give all required info before ask any questions. Don't post any login details here. Oct 28, 2019 at 3:29
  • @YuanLim Updated the answer and added sample code for wpdb to get data from custom table.
    – Hector
    Oct 28, 2019 at 4:33

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.