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

How to order posts by the user_login that corresponds to a meta_value (which is a user id)?

Ok, solved this. I had to use the posts_clausesfilter and the following code:

function assigned_to_orderby( $clauses, $wp_query ) {
    global $wpdb;

    $clauses['join'] .= "
    LEFT OUTER JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
    LEFT OUTER JOIN {$wpdb->users} ON {$wpdb->users}.ID={$wpdb->postmeta}.meta_value
    ";

    $clauses['where'] .= " AND ({$wpdb->postmeta}.meta_key = 'assignee')";
    $clauses['groupby'] = "{$wpdb->postmeta}.post_id";
    $clauses['orderby']  = "GROUP_CONCAT({$wpdb->users}.user_login ORDER BY {$wpdb->users}.user_login ASC) ";
    $clauses['orderby'] .= ( 'DESC' == strtoupper( $_GET['order'] ) ) ? 'DESC' : 'ASC';

    return $clauses;
}

Adapted from http://scribu.net/wordpress/sortable-taxonomy-columns.html

Related Posts:

  1. How to order users alphabetically by their last name?
  2. meta_query orderby sort multiple keys
  3. Sort users by custom user meta value
  4. alphabetically order role drop-down selection in dashboard
  5. order users with drag’n’drop?
  6. Getting users who registered 360 days from current date
  7. Querying Email Addresses for a List of Users with Same Last Name?
  8. How to delete users with no order history through phpmyadmin sql?
  9. What is proper way to store info such as user marking a post as favorite? In post meta or user meta?
  10. Column Sorting Code on users.php
  11. How change user type from contributor to author
  12. WordPress SQL Issue not returning correct reselts
  13. cannot get user_registered date from get_user_meta
  14. How to copy user_nicename value into user_login
  15. How can I prevent certain custom roles from seeing other custom roles on the user list page?
  16. Sort custom posts in archive by multiple values: date AND meta key
  17. SQL Bulk update all WordPress user’s nicknames to firstname + lastname format
  18. Get usermeta info from sql query
  19. SELECT from wp_users, get Displayname too
  20. how to delete in phpmyadmin sql – all users with certain condition and all dependencies
  21. How to get image path from id using SQL
  22. SQL to set Display Name to First Name + Last Name
  23. sort title descending with title with number
  24. Safely changing UserID’s, re-using deleted UserID’s and automatically using deleted userID’s instead of an increment
  25. Insert query not working for non-logged in user
  26. User list order by user meta
  27. Sorting Users page admin column with ACF field
  28. I try to add informations to User profile
  29. List all users based on array of domains
  30. Users set passwords but cannot login
  31. Store extra user values permanently
  32. Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
  33. Custom post order when using OR relation
  34. How do I change the user via SQL?
  35. post acces for guests / unregistered users only
  36. Add method for tracking who referred new users
  37. How do I Limit the number of pages a non-subscriber can see?
  38. Track users views
  39. user added by(who added this user)
  40. New User ID not working in custom function with user_register hook
  41. Add an ‘edit profile’ page with Genesis
  42. Resend user activation mail
  43. Exclude Current user email and send notification
  44. Registered access area
  45. Cloning admin pages like users.php
  46. Username has been exposed
  47. force registered user as seller
  48. Custome column sort by date not title
  49. Subscriber role – blank page
  50. Set user role on registration so can upload file to own media library area
  51. Display Users and user url’s
  52. custom login form, guide me
  53. Ordering list of users by last name and then by first name
  54. Edit dashboard based on user ID
  55. What’s the most secure way to grant a user permission to update in a multisite?
  56. is_user_logged_in() not working after domain change
  57. User email not being changed (cached?) until next page load (frontend)
  58. Error when send reset password
  59. Manage user profiles with WordPress
  60. Separate Profiles for Users
  61. How To Find The ID Of All Registered Users?
  62. WP Query to order posts by multiple meta fields
  63. Not able to call value in the core files
  64. How to get only 1 role from user
  65. Get the author registration date in the header.php file
  66. Front end login and page restriction
  67. How to disable users changing their display_name?
  68. User “none” role
  69. Re-order posts inside tax query
  70. Posts with no meta field do not appear when sorting by meta field
  71. User count only for role frontend vendor
  72. Disallow user registration/checkout via Hotmail/Yahoo
  73. logged_in user outside of wordpress loop
  74. Query category-specific, paginated posts and allow viewer to change sort order
  75. Pre_Get_Posts order DESC not registering
  76. Limit roles displayed in users.php depending on custom role
  77. User management missing after migration to new host
  78. Sorting: custom query with orderby meta_value_num THEN by title
  79. query_posts with sorting on a custom datestamp
  80. Add new user and user bio at same time
  81. Fetching posts that match a User Profile setting
  82. Migrating Users along with their password
  83. How to create a link to a user’s profile page
  84. How to sort post_meta in edit.php?
  85. show count author post today
  86. orderby in WP_query doesn’t works
  87. How i will get the images of highest scored users?
  88. Are there any hook or filter when adding new user in WordPress?
  89. Sorting meta box values by start- and enddate and all dates in between
  90. Creating Custom user type just like custom post
  91. Validate user meta and redirect
  92. Retrieve New user’s ID
  93. Add a column before username in the users profile table
  94. Designing a member area on my site
  95. Link to Authors blog posts
  96. Changing user_login ends current session
  97. Let admin users edit member profiles from front end
  98. Will mass deleting WP Users then reimporting CSV with the same userid break WP?
  99. view and update form only for registered users
  100. Does WordPress Need BuddyPress to Handle Large Amounts of Users?
Categories users Tags order, post-meta, sort, sql, users
Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
setCookie before headers are sent?

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