• Resolved Tom Combs

    (@tcwebguru)


    Hello all! I’ve posted here in the past and had great success, hoping for the same today.

    I have a wordpress site and I’m trying to pull the members into a page and sort them by results.
    It’s a Musicians site and I need to be able to sort the members by the instruments they play.

    This code will pull the members into the page and you can see the results at: https://afm1.org/wp/?page_id=677

    <?php
    $args = array(
        'fields' => 'all','role' => 'Subscriber',
        'meta_query' => array(
             array(
             'key'=>'instrument'
             ))
    );
    $members = get_users($args);
    //custom function for comparing the data we want to sort byfunction cmp($a, $B){
    if ($a->instrument == $b->instrument) {
    return 0;
    }
    return (
    $a->instrument > $b->instrument) ? 1 : -1;
    }
    
    usort($members, 'cmp');
    
    foreach ($members as $member ) {
    // get all the user's data
    $member_info = get_userdata($member->ID);
    echo '<div id="membox">';
    echo $member_info->first_name. ' ' .$member_info->last_name.'<br />';
    echo $member_info->address_1.'<br />';
    echo $member_info->city.', '.$member_info->state.', '.$member_info->zip.'<br />';
    echo $member_info->phone.'<br />';
    echo '<a>email.'">'.$member_info->email.'</a><br />';
    foreach ($member_info->instrument as $inst) {
    echo '<b>'.$inst.'</b>, '; }echo '</div>';
    }
    ?>

    What I want to do is have the Instrument as a title and have all of the members that play that instrument listed like this:

    <h1>Accordian</h1>
    Arpi Anderson
    1234 Whitfield Avenue
    Cincinnati, OH, 45220
    513-111-1111
    [email protected]
    Accordion,

    David Abbott
    123 N. Ft. Thomas Ave.
    Ft. Thomas, KY, 41075
    859-111-1111
    [email protected]
    Accordion, Acoustic,

    <h1>Violin</h1>
    Ann Baer
    123 Cabinridge
    Batavia, OH, 45103
    513-111-1111
    [email protected]
    Violin,

Viewing 4 replies - 16 through 19 (of 19 total)
  • confirmed, even works with my additional filter to only show users with posts..

    Thread Starter Tom Combs

    (@tcwebguru)

    Thanks so much for jumping in to help out. i’m like you, I hate when I can’t figure something out.

    I also posted at phpfreaks.com and I got some help there too.
    Here’s what I ended up with, it’s working perfectly.

    <?php
    $args  = array(
      'fields' => 'all',
      'role' => 'Subscriber',
      'meta_query' => array(
      					array(
    						'key'=>'instrument'
    						))
      );
    $members = get_users($args);
    
    //custom function for comparing the data we want to sort by
    function cmp($a, $b){
      if ($a->instrument == $b->instrument) {
        return 0;
      }
      return ($a->instrument > $b->instrument) ? 1 : -1;
    } 
    
    usort($members, 'cmp'); 
    
    $directory = array();
    foreach ($members as $member ) { // get all the user's data
    $member_info = get_userdata($member->ID);
    foreach ($member_info->instrument as $inst) {
    $directory[$inst][] = array (
    'lastname' => $member_info->last_name,
    'firstname' => $member_info->first_name,
    'address' => $member_info->address_1,
    'city' => $member_info->city,
    'state' => $member_info->state,
    'zip' => $member_info->zip,
    'phone' => $member_info->phone,
    'email' => $member_info->email
    );
    }}
    ksort($directory);  // sort by instrument
    foreach ($directory as $instrument => $players) {
    echo '<h2>' .$instrument. '</h2>';
    sort($players);  // sort players by lastname
    foreach ($players as $player) {
    		echo '<div id="membox">';
    		echo $player['firstname']. ' ' .$player['lastname'].'';
    		echo $player['address'].'';
    		echo $player['city'].', '.$player['state'].', '.$player['zip'].'';
    		echo $player['phone'].'';
    		echo '<a href="mailto:'.$player['email'].'">'.$player['email'].'</a>';
    		echo '<b>'.$instrument.'</b>, ';
    		echo '</div>';
     }}
    ?>

    [moderated: please ensure that your code is enclosed in backticks (`) or use the code button.]

    haha, I think my job was harder trying to make it work in an existing custom authors page, I had to change $users to $authors in a few places and got lucky. still can’t get the value to display with the authors but I’ll get it eventually.

    Thanks, that was fun and I think I learned somethig…

    Thread Starter Tom Combs

    (@tcwebguru)

    Excellent! Glad we were able to work together on this!

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Sorting members array by meta value’ is closed to new replies.