Sorting members array by meta value
-
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,
- The topic ‘Sorting members array by meta value’ is closed to new replies.