Buddypress ORDER BY custom sort option on members directory
<p>I have implemented some custom sort options on the member directory of buddypress.  The one I am having an issue with is sort by Last Name (a xprofile field).  When I <code>print_r</code> the results of the query to the screen the user ID's are in the order I want them displayed in but when I visit the page the members are not in that order.  Reading up on bp_ajax_querystring I see you can pass ORDER and ORDERBY but anything I have tried doesnt work.  The members should display in alphabetical order by last name.</p>

<p>Here is the piece of code with the query.</p>

<pre><code>if( $ch_querystring['type'] == 'alphabetical-last' ){

        global $wpdb;

        $field_id = 518; //Last name field ID

        $query = "SELECT user_id FROM " . $wpdb-&gt;prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " ORDER BY " . $wpdb-&gt;prefix . "bp_xprofile_data.value ASC";

        $custom_ids = $wpdb-&gt;get_col( $query );

        // convert the array to a csv string
        $user_str = implode(",", $custom_ids);

        echo '&lt;pre&gt;';
        print_r($user_str); //Prints the ID's I would like the members to be displayed in
        echo '&lt;/pre&gt;';

        $ch_querystring['include'] = $user_str; 
        //$ch_querystring['type'] = 'alphabetical'; 
        $ch_querystring['per_page'] = 500;
        $ch_querystring['exclude'] = '1,1101,1030';
        //$ch_querystring['orderby'] = "ORDER BY FIELD(value," . $ch_querystring['include'] . ")";  //Doesnt Work
        //$ch_querystring['order'] = 'DESC'; //ASC and DESC doesnt work
        echo '&lt;pre&gt;';
        print_r($ch_querystring);
        echo '&lt;/pre&gt;';

        return $ch_querystring;

    }
</code></pre>

<p>For debugging here is what <code>$ch_querystring</code> returns:</p>

<pre><code>Array
(
    [type] =&gt; alphabetical-last
    [action] =&gt; alphabetical-last
    [scope] =&gt; all
    [page] =&gt; 1
    [user_id] =&gt; 0
    [search_terms] =&gt; 
    [exclude] =&gt; 1,1101,1030
    [per_page] =&gt; 500
    [include] =&gt; 1511,1499,1477,1483,1,1512,1510,1482,1503,1514,1498,1502,1506,1484,1479,1492,1497,1488,1495,1508,1475,1481,1501,1515,1478,1491,1490,1505,1487,1509,1480,1507,1485,1493,1513,1500,1516,1496,1476,1489,1504,1494,1486,1474
    [orderby] =&gt; ORDER BY FIELD(value,1511,1499,1477,1483,1,1512,1510,1482,1503,1514,1498,1502,1506,1484,1479,1492,1497,1488,1495,1508,1475,1481,1501,1515,1478,1491,1490,1505,1487,1509,1480,1507,1485,1493,1513,1500,1516,1496,1476,1489,1504,1494,1486,1474)
)
</code></pre>

<p>Here is a link to a <a href="http://pastebin.com/53sN5gLu" rel="nofollow">paste with the full code</a> in case it helps</p>