Skip to:
Content
Pages
Categories
Search
Top
Bottom

List Buddpyress users ordered by meta_value


  • enderpal444
    Participant

    @enderpal444

    I’m trying to make a list of Buddypress members ordered by a custom meta_value. I tried using `get_users()` but it doesn’t allow meta_value to pass through orderby. What’s a way to actually do this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m not sure. meta_value isn’t an indexed field in the database, so I’d be very surprised if this was supported in WordPress (it wouldn’t scale efficiently to a large site/database).


    enderpal444
    Participant

    @enderpal444

    Yeah that’s a concern. I have a meta value for each user which is a score for that user so I have a need to show list of top 10 scores and stuff like that.


    Roger Coathup
    Participant

    @rogercoathup

    Using the WP API you can sort by meta_value on posts (WP_Query), but not on users (WP_User_Query).

    You’d either have to do it in 2 passes, or write your own custom SQL statement.

    If you have a lot of users, and scores change infrequently, you might want to consider caching your results for a period.


    enderpal444
    Participant

    @enderpal444

    @djpaul @rogercoathup I was able to do it with the following code which seems to be a nice solution for scaling.

    <?php
    //these are the arguments for the get_users function below
    $args  = array(
    'fields' => 'all_with_meta',
    'role' => 'author',
    'meta_query' => array(
    array(
    'key' => 'score', // the meta field (or key) we want to target
    )
    ));
    //get_users calls WP_User_Query and returns an array of matching users
    $users = get_users($args);
    //custom function for comparing the data we want to sort by
    function cmp($a, $b){
    if ($a->score == $b->score) {
    return 0;
    }
    return ($a->score > $b->score) ? -1 : 1;
    }
    //usort sorts our $users array with our function cmp()
    usort($users, 'cmp');
    //leaving an array of $users sorted by the value of meta 'points'
    foreach ($users as $user) {
    echo '<li>';
    echo get_avatar( $user->ID, 30 );
    echo '<span>' . $user->display_name . '</span>';
    echo '</li>';
    } ?>

    Roger Coathup
    Participant

    @rogercoathup

    @enderpal444 – thanks for posting your solution – that’s the sort of approach I was suggesting as ‘two pass’ – getting all the users unsorted, then sorting them, before iterating over them.

    Efficiency might become an issue if your member numbers increase.

    hi @enderpal444

    I think i would use BP members loop to do what you want to do as you can send meta_key to BP_Core_User::get_users. Then i would take benefit of the filters included in the 2 sql to change its behavior. This is what i’ve tried to add a filtering by geo position to the main BP Members directory. I guess you’ll need to ask Roger and Paul about efficiency…

    in the functions.php of my active theme > http://pastebin.com/2HKsSZCc

    i tried to have a ‘one pass’ approach ;)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘List Buddpyress users ordered by meta_value’ is closed to new replies.
Skip to toolbar