WordPress.org

Forums

Sort users by meta_value_num (2 posts)

  1. andy999
    Member
    Posted 1 year ago #

    Been trying some things to output some users and sort by a meta_value_num, but nothing I try works and I can't find anything online about it.

    This one's a real challenge.

    <?php
    $args = array(
        'orderby' => 'meta_value_num',
        'meta_key ' => 'rank',
        'order' => 'DESC',
    );
    
    // The Query
    $user_query = new WP_User_Query( $args );
    
    // User Loop
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            echo '<p>' . $user->display_name . '</p>';
        }
    } else {
        echo 'No users found.';
    }
    ?>

    I can get the meta value for each author on author.php easily enough but I can't get users to sort by the same meta value.

    [Moderator Note: No bumping. If it's that urgent, consider hiring someone instead.]

  2. Steven Jones
    Member
    Posted 1 year ago #

    The codex says you only need to use 'meta_value' not 'meta_value_num', it could be that.

    Also, for you to sort by a meta key, the meta value needs to exist for all users. If they user does not have a meta value for that key then won't be returned in the results.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.