Skip to:
Content
Pages
Categories
Search
Top
Bottom

Member loop – Create member list via custom field + custom order


  • AwesomeWebGuy
    Participant

    @awesomewebguy

    I don’t think this is clearly explained (I couldn’t find a good walk-through), yet seems to be a requirement by many, so thought would share after few hours trying many suggestions then reading the BuddyPress documentation.

    In our case we wanted to reorder the members list via a custom profile field we’d added called ‘Company’. We wanted only these members to be shown in this list plus ordered via this field alphabetically (I.e we did not want the list to be ordered by active date or name etc).

    The page below contains all the information to build this : https://codex.buddypress.org/developer/bp_user_query/.

    In brief you add a new class to your functions.php and tweak the SQL field_id and value to your requirements.

    For us the ‘Company’ field was using an ID of 2 and we wanted to sort in ascending order, so :

    $custom_ids = $wpdb->get_col(“SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 ORDER BY value ASC”);

    As noted at the bottom of the page, to preserve returned order just use ‘user_ids’ instead of include :

    $query_array->query_vars[‘user_ids’] = $this->custom_ids;

    And that should be it!

Viewing 3 replies - 1 through 3 (of 3 total)

  • amandafrench
    Participant

    @amandafrench

    Thank you SO much, @awesomewebguy!! This worked great for me: I was tearing my hair out trying to figure out how to sort the members directory by an xprofile field. All the good karma to you!


    amandafrench
    Participant

    @amandafrench

    I guess I’m never satisfied, but I do have an additional request / problem, if someone can help (cough @boonebgorges?). Our xprofile fields are geographical: City, State, Country. I’d like to do a sequential sort on them on the Members directory: first ASC by Country (which I’ve now got working, finally, thanks to this post), then second ASC by State, third ASC by City. I’ve mucked around with SQL statements and ORDER BY FIELD and CASE IF THEN, but I can’t get those subsequent sorts to work.

    xprofile field_id values: 1 = Name, 8 = Org, 9 = City, 10 = State, 11 = Country

    Members loop code:

    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&per_page=500' . '&populate_extras&type=alphabetical' . '&exclude=1,2,3,4,6') ) : ?>

    Below code in functions.php works – it sorts members by Country ASC
    custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 11 ORDER BY value ASC");

    Below code in functions.php doesn’t work to sort by Country, State, City ASC – returns an unsorted members list. I think it’s sorting randomly. It isn’t by user id, username, last active, or newest registered. When I apply the default member type sorts, though, with the members-order-select dropdown, that works.

    $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id IN ('1', '8', '9', '10', '11') ORDER BY FIELD (field_id, '11', '10', '9', '8', '1')");

    Any advice for constructing that SQL query? Do I need to look more into bp_parse_args?


    amandafrench
    Participant

    @amandafrench

    Never mind! I’ve found a fabulous solution that has gotten me out of SQL query hell. Again, if anyone else is trying to sort a Members Directory, here’s what I did:

    1) Put it in a table, not a list (I had done this anyway, since I wanted a single-page directory that displayed xprofile fields horizontally) — make sure it has a <th> table header row;
    2) Install the plugin Table Sorter https://wordpress.org/plugins/table-sorter/ and add the class “tablesorter” to the table.

    Et voilĂ ! I commented out the sort type dropdown in members/index.php starting with
    <div class="item-list-tabs" id="subnav" aria-label="<?php esc_attr_e( 'Members directory secondary navigation', 'buddypress' ); ?>" role="navigation">
    all the way through

    <?php
    /**
     * Fires inside the members directory member order options.
     *
     * @since 1.2.0
     */
    do_action( 'bp_members_directory_order_options' ); ?>
    </select>
    </li>
    </ul>
    </div>

    and I made sure that the default single page list showed all users alphabetically by default in members-loop.php

    <?php if ( bp_get_current_member_type() ) : ?>
    	<p class="current-member-type"><?php bp_current_member_type_message() ?></p>
    <?php endif; ?>
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&per_page=500' . '&exclude=1,2,3,4,6' . '&type=alphabetical') ) : ?>

    And then all I had to do was add that tablesorter class to the table as required by the tablesorter plugin, and I got a nice dynamic table with sortable columns, which can also be sorted by multiple criteria by holding down the Shift key on the second column. I set the initial sort order with parameters that select the column by number, as described at http://tablesorter.com/docs/example-option-sort-list.html and elsewhere.

    <table id="members-list" class="tablesorter {sortlist:[[4,0], [3,0], [2,0], [1,0], [0,0]]}" aria-live="assertive" aria-relevant="all">

    Yay.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar