WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sorting members array by meta value (20 posts)

  1. Tom Combs
    Member
    Posted 1 year ago #

    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: http://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
    arpianderson@hotmail.com
    Accordion,

    David Abbott
    123 N. Ft. Thomas Ave.
    Ft. Thomas, KY, 41075
    859-111-1111
    davejanet@acninc.net
    Accordion, Acoustic,

    <h1>Violin</h1>
    Ann Baer
    123 Cabinridge
    Batavia, OH, 45103
    513-111-1111
    violin@cinci.rr.com
    Violin,

  2. deepbevel
    Member
    Posted 1 year ago #

    check out WP User QueryCustom Field Parameters

  3. deepbevel
    Member
    Posted 1 year ago #

    this should list all members by their meta key with the meta value "instrument"

    <?php
      $my_custom_field = $custom_fields['instrument'];//key name
      foreach ( $my_custom_field as $key => $value )
      echo $key . " => " . $value . "<br />";
    ?>

    so it should output:

    Acccrodian
    Acccrodian Acoustic
    Violin

    But you'd need to preserve your other member info for each "member as member" ..so not sure if that's a new query or if they can me merged..

  4. Tom Combs
    Member
    Posted 1 year ago #

    deepbevel, thanks for helping.
    I'm trying your code now, will update in a few.

  5. deepbevel
    Member
    Posted 1 year ago #

    I'm still checking it out, I have a site I can test on. I'll be back if I get anything good.
    This is complicated for me too but it would be great to figure it out.

  6. Tom Combs
    Member
    Posted 1 year ago #

    Here's what I have now. I'm almost where I want to be, however, something is obviously still wrong.

    '<?php

    $args = array(
    'fields' => 'all_with_meta',
    'role' => 'Subscriber',
    'meta_query' => array(
    array(
    'key'=>'instrument'
    ))
    );

    $user_query = new WP_User_Query ($args);

    // User Loop
    if ( !empty( $user_query->results ) ) {

    foreach ( $user_query->results as $category ) {
    foreach ( $category->instrument as $cat ) {
    echo '<h2>' .$cat . '</h2>';

    echo '<div id="membox">';
    echo $category->first_name. ' ' .$category->last_name.'
    ';
    echo $category->address_1.'
    ';
    echo $category->city.', '.$category->state.', '.$category->zip.'
    ';
    echo $category->phone.'
    ';
    echo 'email.'">'.$category->email.'
    ';
    foreach ($category->instrument as $inst) {
    echo '<b>'.$inst.'</b>, '; }
    echo '</div>';
    }
    }} else {
    echo 'No users found.';
    }

    ?>'

    I'm getting them sorted with instruments, but it's only listing 1 member per instrument, and it's listing the same instrument multiple times.
    Check the link and you can see what I mean: http://afm1.org/wp/?page_id=677

  7. deepbevel
    Member
    Posted 1 year ago #

    hmm.. just realized I don't know how to add a custom feild to a members profile. Have to get that first. you'll probably solve before I catch up.
    looks like you're making some progrees though.
    However perhaps it would be ideal if all posts with the same key could be listed under that key..?

  8. deepbevel
    Member
    Posted 1 year ago #

    so, you already concluded the same.. hmm

  9. Tom Combs
    Member
    Posted 1 year ago #

    Yeah, it's almost there.
    not sure if I'm missing an if/else statement, but it's close.

  10. Tom Combs
    Member
    Posted 1 year ago #

    Ok...almost there. Now I have only the member of the instrument showing up. But now I need to combine the instruments, instead of each showing up individually

    '
    <?php

    $args = array(
    'fields' => 'all_with_meta',
    'role' => 'Subscriber',
    'meta_query' => array(
    array(
    'key'=>'instrument'
    ))
    );

    $user_query = new WP_User_Query ($args);

    // User Loop
    if ( !empty( $user_query->results ) ) {

    foreach ( $user_query->results as $category )
    foreach ( $category->instrument as $cat ) {
    echo '<h2>' .$cat . '</h2>';

    foreach ( $user_query->results as $user ) {
    if ( $category->instrument == $user->instrument ) {
    echo '<div id="membox">';
    echo $user->first_name. ' ' .$user->last_name.'
    ';
    echo $user->address_1.'
    ';
    echo $user->city.', '.$user->state.', '.$user->zip.'
    ';
    echo $user->phone.'
    ';
    echo 'email.'">'.$user->email.'
    ';
    foreach ($user->instrument as $inst) {
    echo '<b>'.$inst.'</b>, '; }
    echo '</div>';
    }}
    }} else {
    echo 'No users found.';
    }

    ?>
    '

  11. deepbevel
    Member
    Posted 1 year ago #

    I think we need a way to order this query by value

    <?php $loop = new WP_Query( array('meta_key'=>'instrument',  'orderby' => 'meta_value_num', 'order' => DESC ) ); while ( $loop->have_posts() ) : $loop->the_post(); ?>

    or here's a thread that appears to deal with exactly what you're doing, sorry I can't test.

    http://wordpress.org/support/topic/list-posts-by-key?replies=22

  12. deepbevel
    Member
    Posted 1 year ago #

    or maybe try just adding

    'orderby' => 'meta_value_num'

    to the array.

  13. deepbevel
    Member
    Posted 1 year ago #

    geez, you got it! just as I finished adding my custom feild to the user profiles..

    Please post your solution

  14. Tom Combs
    Member
    Posted 1 year ago #

    We're almost there. You may notice, that even though the categories look right, the same member appears in each one.

    here is the code for where I am now:
    '
    <?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 '<h1>' .$instrument. '</h1>';
    sort($players); // sort players by lastname
    foreach ($players as $player) { // output player details here
    echo '<div id="membox">';
    echo $member_info->first_name. ' ' .$member_info->last_name.'
    ';
    echo $member_info->address_1.'
    ';
    echo $member_info->city.', '.$member_info->state.', '.$member_info->zip.'
    ';
    echo $member_info->phone.'
    ';
    echo 'email.'">'.$member_info->email.'
    ';
    foreach ($member_info->instrument as $inst) {
    echo '<b>'.$inst.'</b>, '; }
    echo '</div>';
    }}
    ?>
    '

  15. deepbevel
    Member
    Posted 1 year ago #

    I'm looking and i can't see why it would list anyone twice.., this code is weird to me, terms I've never seen.. I'm really only used to dealing with posts.. but I hoped to glean some understanding trying to help out with it. Good luck, hopefully someone else will chime in, or you'll get it yourself.
    And please post if you get it.

  16. deepbevel
    Member
    Posted 1 year ago #

    progress report (couldn't leave it alone)
    looks like I got it
    http://www.foursticks.net/only-a-test/?page_id=2075

    both members have a custom feild key of "twitter" and value "Ihavenotweets"

    However it does not list the name of the value with the users

    http://pastebin.com/9VVwA99s

  17. deepbevel
    Member
    Posted 1 year ago #

    confirmed, even works with my additional filter to only show users with posts..

  18. Tom Combs
    Member
    Posted 1 year ago #

    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.]

  19. deepbevel
    Member
    Posted 1 year ago #

    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...

  20. Tom Combs
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic