• Hi There,

    I’m using the plugins user-list and user-groups and i managed to list the users per group using shortcodes found on the following site:

    https://www.ads-software.com/support/topic/plugin-user-groups-list-group-members?replies=7

    I changed the code that was pasted in functions.php a little bit and added css to it so the users would show up in avatars and names next to eachother.

    The only problem i have is that the users are listed by ID and not by username of surname, so the aren’t alphbetic at the moment.

    Is there anyone how knows how to change the code beneath so the user lists show by username of surname?

    Thanx in advance!

    Code:

    // Grouplist voor de plugin User Groups dmv shortcode [group-list group="NameOfGroup"]
    add_shortcode('group-list', 'my_group_list_shortcode');
    function my_group_list_shortcode( $atts ) {
        // Get the global $wpdb object
        global $wpdb;
    
        // Extract the parameters and set the default
        extract ( shortcode_atts( array(
            'group' => 'No Group' // No Group is a defined user-group
            ), $atts ) );
    
        // The taxonomy name will be used to get the objects assigned to that group
        $taxonomy = 'user-group';
    
        // Use a dBase query to get the ID of the user group
        $userGroupID = $wpdb->get_var(
                        $wpdb->prepare("SELECT term_id
                            FROM {$wpdb->terms} t
                            WHERE t.name = %s", $group));
    
        // Now grab the object IDs (aka user IDs) associated with the user-group
        $userIDs = get_objects_in_term($userGroupID, $taxonomy);
    
        // Check if any user IDs were returned; if so, display!
        // If not, notify visitor none were found.
        if ($userIDs) {
            $content = "<div class='entry clearfix'>";
            foreach( $userIDs as $userID ) {
                $user = get_user_by('id', $userID);
                $content .= "<div class='group-list'>";
    			$content .= get_avatar( $user->ID, 70 );
                $content .= "" . $user->display_name . "";
            	$content .= "</div>";
    }
            $content .= "</div>";
        } else {
            $content =
            "<div class='group-list group-list-none'>Geen resultaten zichtbaar</div>";
    
        }
        return $content;
    }

    I use the following shortcode in my article:

    [group-list group="GROUPNAME"]

    Hope someone kan help me!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter bvstrien

    (@bvstrien)

    Isn’t there anybody how knows how to change the sorting?

    Moderator keesiemeijer

    (@keesiemeijer)

    Never used the plugin, but this should sort it by display name:

    // sorting function
    function compare_display_name( $a, $b ) {
        return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
    }
    
    // Grouplist voor de plugin User Groups dmv shortcode [group-list group="NameOfGroup"]
    add_shortcode( 'group-list', 'my_group_list_shortcode' );
    function my_group_list_shortcode( $atts ) {
        // Get the global $wpdb object
        global $wpdb;
    
        // Extract the parameters and set the default
        extract( shortcode_atts( array(
                    'group' => 'No Group' // No Group is a defined user-group
                ), $atts ) );
    
        // The taxonomy name will be used to get the objects assigned to that group
        $taxonomy = 'user-group';
    
        // Use a dBase query to get the ID of the user group
        $userGroupID = $wpdb->get_var(
            $wpdb->prepare( "SELECT term_id
                            FROM {$wpdb->terms} t
                            WHERE t.name = %s", $group ) );
    
        // Now grab the object IDs (aka user IDs) associated with the user-group
        $userIDs = get_objects_in_term( $userGroupID, $taxonomy );
    
        // Check if any user IDs were returned; if so, display!
        // If not, notify visitor none were found.
        if ( $userIDs && !is_wp_error( $userIDs ) ) {
    
            $users = array();
    
            // put all users in an array that we can sort
            foreach ( $userIDs as $userID ) {
                $users[] = get_user_by( 'id', $userID );
            }
    
            // sort by display name
            usort( $users, "compare_display_name" );
    
            $content = "<div class='entry clearfix'>";
            foreach ( $users as $user ) {
                $content .= "<div class='group-list'>";
                $content .= get_avatar( $user->ID, 70 );
                $content .= "" . $user->display_name . "";
                $content .= "</div>";
            }
            $content .= "</div>";
    
        } else {
            $content =
                "<div class='group-list group-list-none'>Geen resultaten zichtbaar</div>";
        }
    
        return $content;
    }

    Thread Starter bvstrien

    (@bvstrien)

    GREAT! That did the trick! Thanx a lot ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List users by username or surname?’ is closed to new replies.