我在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)
)
以下是paste with the full code的链接,以防
答案 0 :(得分:0)
我需要一个辅助函数来使用类似的东西对查询进行排序: https://buddypress.org/support/topic/sort-user-list-by-last-name/
{{1}}