Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
1  
Please, post the code you are using. –  brasofilo Feb 2 '13 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? –  ZainShah120 Feb 2 '13 at 22:06
    
Search for a scribu's article about this. Without seeing your code, no meaningful answer can be provided... –  brasofilo Feb 2 '13 at 23:24
add comment

1 Answer

up vote 1 down vote accepted

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 */
    unset($contactmethods['aim']);
    unset($contactmethods['yim']);
    unset($contactmethods['jabber']);

    /* 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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