I have wp_users table which has a column ordering.I came to know that get_users() returns all the users. I am using it like get_users ('orderby=ordering') I got help form this link

But unfortunately it is not sorting on ordering column. Any help?

Here is my code

foreach (get_users ('orderby=ordering') as $user)
                                $user = /* Get full WP_User object instance. */ new WP_User ($user->ID);
  • 1
    Please, post the code you are using.
    – brasofilo
    Commented Feb 2, 2013 at 21:54
  • @brasofilo I just came to know that if you add a custom column in wp_users it will n't do sort on that column..Is there any other way? Commented Feb 2, 2013 at 22:06
  • Search for a scribu's article about this. Without seeing your code, no meaningful answer can be provided...
    – brasofilo
    Commented Feb 2, 2013 at 23:24

1 Answer 1


I think modifying the default tables in your WordPress install is a bad idea. Generally you should never modify the database or the core code, but instead use API functions to get the desired functionality.

In this case that you be using the user meta fields to store a sort order for each user. The easiest way to do this would be to add a field to the user profile screen, here's how I've done it in a plugin:

function my_user_contactmethods($contactmethods, $user = null)
    /* Standard WordPress fields which we don't need */

    /* Now let's add the new fields */
    $contactmethods['company'] = __( 'Company name', 'my_domain' );
    $contactmethods['title'] = __( 'Title', 'my_domain' );
    $contactmethods['sort_order'] = __( 'Order', 'my_domain' );

    return $contactmethods;
add_filter('user_contactmethods', 'my_user_contactmethods', 10, 2);

Off course you could also set the values using update_user_meta if you prefer, or if you simply don't need the input fields. Unfortunately WP_User_Query doesn't support ordering by meta_value like WP_Query does, but by hooking on to pre_user_query we can add this to the standard query.

function my_pre_user_query($query) {

    global $wpdb;

    $meta_query = new WP_Meta_Query();
    // This is just to construct a calid Meta Query, we dont' really run it
    $meta_query->parse_query_vars( array('meta_key' => 'sort_order') );

    if ( !empty( $meta_query->queries ) ) {
        // This requires that the users has meta_values for sort order
        $clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $query );
        $query->query_from .= $clauses['join'];
        $query->query_where .= $clauses['where'];
        $query->query_fields = 'DISTINCT ' . $query->query_fields;

    // This is where the ordering is applied, finally 
    $query->query_orderby = "ORDER BY " . $wpdb->usermeta . ".meta_value" . " " . $query->query_vars['order'];
add_action('pre_user_query', 'my_pre_user_query');

Since this will affect all user queries, only add the action where appropriate and remove once it's not needed anymore.

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.