Buddypress ORDER BY成员目录上的自定义排序选项


Buddypress ORDER BY custom sort option on members directory

我在buddypress的成员目录上实现了一些自定义排序选项。我遇到的问题是按姓氏排序(一个xprofile字段)。当我将查询结果print_r显示到屏幕时,用户ID按我希望的顺序显示,但当我访问页面时,成员不按该顺序显示。通过阅读bp_ajax_querystring,我发现您可以传递ORDER和ORDERBY,但我尝试过的任何方法都不起作用。成员应按姓氏字母顺序显示。

这是一段带有查询的代码。

if( $ch_querystring['type'] == 'alphabetical-last' ){
        global $wpdb;
        $field_id = 518; //Last name field ID
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " ORDER BY " . $wpdb->prefix . "bp_xprofile_data.value ASC";
        $custom_ids = $wpdb->get_col( $query );
        // convert the array to a csv string
        $user_str = implode(",", $custom_ids);
        echo '<pre>';
        print_r($user_str); //Prints the ID's I would like the members to be displayed in
        echo '</pre>';
        $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 '<pre>';
        print_r($ch_querystring);
        echo '</pre>';
        return $ch_querystring;
    }

对于调试,这里是$ch_querystring返回的内容:

Array
(
    [type] => alphabetical-last
    [action] => alphabetical-last
    [scope] => all
    [page] => 1
    [user_id] => 0
    [search_terms] => 
    [exclude] => 1,1101,1030
    [per_page] => 500
    [include] => 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] => 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)
)

这里有一个粘贴完整代码的链接,以防它有助于

我需要一个助手函数来使用类似的东西对查询进行排序:https://buddypress.org/support/topic/sort-user-list-by-last-name/

//Helper for sorting by last name
function alphabetize_by_last_name( $bp_user_query ) {
    if ( 'alphabetical-last' == $bp_user_query->query_vars['type'] )
        $bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)";
}
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );