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 use the following shortcode and function to display members of some departments:

add_shortcode( 'list_of_members', 'members_listing' );
/* usage: [list_of_members department = 'psychology'] */

function members_listing( $department ) {

    $members = get_users( array( 'meta_key' => 'department', 'meta_value' => $department ) ); 

    echo '<ul>';
    foreach ( $members as $member ) {
        echo '<li>' . $member->first_name . ' ' . $member->last_name . '</li>';
    }
    echo '</ul>';
}

I'd like to order the users alphabetically by last_name. How can I do this?

I was inspired by this question/answer, but I do not understand it completely.

share|improve this question
    
You get the result without ordering? –  Bindiya Patoliya Sep 4 '13 at 9:00
    
Essentially a duplicate of this question: wordpress.stackexchange.com/q/107406/21376 –  s_ha_dum Sep 4 '13 at 14:02
    
@bindiya-patoliya Yes, you can see it here. –  Iurie Sep 4 '13 at 16:42
add comment

3 Answers

up vote 2 down vote accepted

Why don't use built-in functionality of PHP?

Put the following line right before the foreach:

usort($members, create_function('$a, $b', 'return strnatcasecmp($a->last_name, $b->last_name);'));

References:

share|improve this answer
    
I added your first line of code (before of your EDIT) to my function and I got the desired result, without to touch the rest of code, this just works. Your edited version of this function (after of your EDIT) also works, but it has two foreach. I don't get the fields first_name and last_name because they exists in the $members variable (in my code) or in the $users variable (in your code), from user meta data. Or not? I am not at all a coder, so maybe I do not understand. Anyway, thank you! –  Iurie Sep 4 '13 at 17:25
    
Glad it worked out (that easily). I just wasn't that sure about the fields you get from get_users. However, I now confirmed (for myself) that you get an array of WP_User objects - which have the first_name and last_name field/property. I was unsure because of this return example. So, you are absolutely right to use the short version. Anyway, glad I could help. –  t f Sep 4 '13 at 18:53
add comment

Add orderby=>last_name,order=>DESC in your get_users argument. Hope this will help you.

$members = get_users( array( 'meta_key' => 'department', 'meta_value' => $department, 'orderby' => 'last_name', 'order' => 'DESC' ) ); 
share|improve this answer
    
According with get_users documentation order_by=>'last_name' is not a valid value but it can works, I'd like to know if it is working. –  cybnet Sep 4 '13 at 10:18
1  
Sorry, this can't possibly work. The valid orderby values are hard-coded and last_name isn't one of them. Pass a wrong value and you default to user_login. –  s_ha_dum Sep 4 '13 at 13:50
    
@cybnet Your code works, the order has changed, but without any sense (not alphabetical order). –  Iurie Sep 4 '13 at 16:57
    
See my previous comment where I talked about my doubts and the comment of @s_ha_dum who confirmed my doubt. order_by=>'last_name' is not valid and is replaced by the default order which is user_login. You will have to perform the query and reorder the users array using built-in PHP function as suggested by @tf in his answer. –  cybnet Sep 4 '13 at 17:23
add comment

I modified your function:

add_shortcode( 'list_of_members', 'members_listing' );
/* usage: [list_of_members department = 'psychology'] */

function members_listing( $department ) {

   $members = get_users( array( 'meta_key' => 'department', 'meta_value' => $department, 'orderby' => 'last_name', 'order' => 'DESC' ) ); 

   echo '<ul>';
   foreach ( $members as $member ) {
      echo '<li>' . $member->first_name . ' ' . $member->last_name . '</li>';
   }
   echo '</ul>';
}

Just added two more parameters two your arguments

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.