4

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.

3

3 Answers 3

7

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:

2
  • 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!
    – Yurié
    Sep 4, 2013 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.
    – tfrommen
    Sep 4, 2013 at 18:53
-1

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

2
  • 1
    It would be nice if this worked, but last_name isn't a valid value to pass to orderby. :(
    – Chris Rae
    Nov 10, 2014 at 18:26
  • Chris you are right. For others please do not follow my above answer.
    – Manish
    Nov 11, 2014 at 18:50
-1

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' ) ); 
4
  • 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.
    – cybmeta
    Sep 4, 2013 at 10:18
  • 2
    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, 2013 at 13:50
  • @cybnet Your code works, the order has changed, but without any sense (not alphabetical order).
    – Yurié
    Sep 4, 2013 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.
    – cybmeta
    Sep 4, 2013 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.