I’m building a WordPress site where I need to sort the results of a WP_User_Query AFTER the query has already been run. For those of you not familiar, it has elements that look like this:

Array (
  [0] => WP_User Object (
    [data] => stdClass Object (
      [ID] => 1
      [user_login] => MarvinLazer
      [user_pass] => $P$BUGHRCjMzlvn7dlGp53UTPC8GMF081/
      [user_nicename] => marvinlazer
      [user_email] => marvin@lazer.com
      [user_url] => http://marvinlazer.com
      [user_registered] => 2017-03-04 23:08:08
      [user_activation_key] =>
      [user_status] => 0
      [display_name] => Marvin Lazer
    )
    [ID] => 1
    [caps] => Array (
      [subscriber] => 1
    )
    [cap_key] => wp_capabilities
    [roles] => Array (
      [0] => subscriber
    )
    [allcaps] => Array (
      [read] => 1
      [level_0] => 1
      [subscriber] => 1
    )
    [filter] =>
  )
  [1] => WP_User Object ( etc. etc.

Based on this very helpful page on PHP array sorting, I feel like I got something kinda close. Unfortunately, it’s just giving me a blank page after the part where the code appears.

          function cmp(array $a, array $b) {
              if ($a['data']['display_name'] < $b['data']['display_name']) {
                  return -1;
              } else if ($a['data']['display_name'] > $b['data']['display_name']) {
                  return 1;
              } else {
                  return 0;
              }
          }

          usort($user_query->results, 'cmp');

Anyone wanna point me towards what I’m doing wrong?

Read more here: How do I sort a 3-dimensional array that includes objects?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: