Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Get meta value and associated user meta

Just alter your database query to also grab the user ID, and then return an array of data objects (as opposed to just the meta value):

function get_meta_values( $key, $type="workout", $status="publish" ) {
    global $wpdb;

    if ( empty( $key ) )
        return;

    $r = $wpdb->get_results(
        $wpdb->prepare( "
            SELECT pm.meta_value, p.post_author FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s' 
            AND p.post_status="%s" 
            AND p.post_type="%s"",
            $key,
            $status,
            $type
        )
    );

    if ( $r ) {
        $user_ids = wp_list_pluck( $r, 'post_author' );
        cache_users( $user_ids );
    }

    return $r;
}

$workout_leader = get_meta_values( '1_rep', 'workout' );
foreach ( $workout_leader as $data ) {
    echo $data->meta_value;
    echo $data->post_author; // User ID

    // Any other user-related data.
    echo get_the_author_meta( 'display_name', $data->post_author );
}

Related Posts:

  1. Is there a way to append meta data without creating a race condition?
  2. how to get list of all users and their metadata
  3. add_user_meta() vs update_user_meta()
  4. How to make a custom column on the Users admin screen sortable?
  5. wp_update_user doesn’t update and update_user_meta does
  6. Get basic Image Uploader on User Profile Page
  7. Return all users with a specific meta key
  8. Capabilities Vs User Meta
  9. Problem storing arrays with update_user_meta
  10. wp_get_current_user always returns 0
  11. get_users is expecting unserialized meta_value
  12. Can I count the number of users matching a value in a multiple value key?
  13. Content hooks vs User hooks
  14. remove user_meta data from database for all users
  15. How to get last login Access Date and time
  16. How to get user metadata for social media url?
  17. Admin user edit – Grab newly submitted user meta immediately after update/submit
  18. how to get specific fields from get_user_meta()
  19. Validating a new user registration field
  20. Check if WP_User_Query ‘include’ is empty
  21. User meta to post
  22. wp_get_current_user in custom file returns 0
  23. Is there a limit to the length/size of serialized data that can be stored as user meta?
  24. Add profile field (usermeta) to Add New User
  25. Creating a custom post type upon registration
  26. very last action before during user registration before he is redirected to homepage
  27. Customizing WP tables or adding new ones?
  28. How to Make admin Sidebar Menu always be Collapse by code
  29. Are the default entries for a user in wp_usermeta documented?
  30. get_users / WP_User_Query returns empty when logged out
  31. update_user_meta adding new rows in db for same key
  32. How to query users by meta value, even when meta key doesn’t exist?
  33. What is the meta-box-order_post_hash used for?
  34. Job of meta_key meta_value fields in database tables
  35. Add a member number to new user
  36. Allow role to edit users at lower level with only specific metadata
  37. Hook or action to save profile fields at specific time
  38. User meta conventions / name registry, for social media links
  39. Get user’s ID on logout
  40. How to get the user description with get_users?
  41. get current user not working
  42. Sanitize textarea instead of input
  43. Multiple User-Meta Values in One User-Meta Key
  44. Getting $user_id for the_author_meta outside the loop in multisite
  45. Gravity Custom Merge Tags [closed]
  46. No first_name or user_firstname property in WP_User object
  47. How to insert new element to existing array in usermeta?
  48. Is it possible to store visitors IPs in wp_postmeta table?
  49. Possible bug: update_user_meta is updating two unique meta entries
  50. Update user meta through a front end form
  51. What is the difference between get_the_author_meta() and get_user_meta()?
  52. How to display current_user bio
  53. Is a user_meta research case-sensitive or not
  54. Store user’s registration date as meta
  55. Can I prevent “wp_user_level” from getting added to wp_usermeta on registration?
  56. Get the Google+ and Twitter links – WordPress SEO plugin
  57. How to tell whether a user meta value has increased or decreased [closed]
  58. ACF: How to get the full field name (meta_key) by a field key?
  59. Showing all users who match 2 meta fields with current user
  60. Adding current user’s ID to the end of PDF hyperlinks in post content
  61. how to delete all users and posts based on ‘user_meta’?
  62. update_user_meta() not working
  63. How to display user’s avatar on their profile page?
  64. Send mail to user only once when specific check box is selected and updated from profile meta updated by admin
  65. WP_user_query throws a 404 error
  66. get_*_meta doesn’t always return an array
  67. Incrementally add or substract from usermeta field
  68. add custom field in user register form
  69. Is it possible to read a result if wp_update_user or update_user_meta fails?
  70. set a user-meta key as avatar
  71. Prevent update of custom user profile meta field on the front end after 1st entry
  72. Restrict Access to Posts based on Custom User and Post Meta Data
  73. Creating user status mode in WordPress
  74. User last login and user last visit problem
  75. Why get_users() not working on the admin backend?
  76. Can I set user meta for theoretical user 0?
  77. strange issue with user_meta + json_encode/decode
  78. Inserting and finding multiple values in user-meta fields
  79. Update user meta on logout
  80. update_user_meta after wordpress in action hooks
  81. Adding user data to an existing user_id
  82. I want to update my user meta table
  83. Problem to wp_hash
  84. trying to determine if meta value exists for user and if not auto submit gravity form to redirect
  85. Subscribers Group User meta delete
  86. action user_new_form param is a string
  87. Redirecting users based on custom user meta
  88. Customize Avatar using user meta
  89. get_user_meta returning old data
  90. Why does the usermeta table contain html entities?
  91. get_user_meta returning empty when it’s NOT empty
  92. How can I programmatically show a specific (template) page, like author.php?
  93. WP_User_Query not returning users with meta – what am I missing?
  94. Multiple meta values for one meta key
  95. When is a user’s meta data table loaded?
  96. Display and Update User Custom Meta via user-edit.php
  97. Multiple Email Addresses, One User
  98. WordPress REST API and User meta data
  99. How do I add a dropdown list of numbers 1 – 1000 as an extra profile field?
  100. Get user meta for only the keys with a certain prefix
Categories user-meta Tags post-meta, user-meta
WordPress and magic quotes
If I delete my wordpress site then does that delete all my sites?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress