Support » Fixing WordPress » How to Override orderby to create list of users by custom meta_value

  • I’m having issues ordering a loop of users by a custom meta_value. Reading the Codex for get_users(), it doesn’t actually say that you can use the meta_value for orderby. When I try to do it I get a list of users not ordered by the meta_value.

    <ul>
    <?php
    $args = array(
    'role' => 'author' ,
    'meta_key' => 'score',
    'orderby' => 'meta_value_num',//Tried meta_value also
    'order' => 'DESC',
    'number' => 5,
    );
    $blogusers = get_users($args);
    foreach ($blogusers as $user) {
        echo '<li>' . $user->display_name . '</li>';
        echo get_user_meta($user->ID, 'score',true);
    }
    ?>
    </ul>

    So what I need to do is override the orderby function by hooking into the pre_user_query. I found this snippet but was told not to use create_function. So what is the right way to do this to order a list of users by user meta_value?

  • The topic ‘How to Override orderby to create list of users by custom meta_value’ is closed to new replies.