Reply To: Adding new ‘Order By’ on members loop
@shanebp I tried $ch_querystring['user_ids'] = $users_str; but I think that only works for bp_user_query();. I saw the example before about how to sort by last name by @danbp and I think I should be able to modify this, but I can’t seem to get it to work:
public function alphabetize_by_last_name( $bp_user_query ) {
if ( 'alphabetical' == $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' );
Also, another weird thing is when I change ‘Order By’ to ‘Price’, all of the results show up, but they ignore what is in the search box. I added some code to take the search parameter in the URL and if you reload the page with the search parameter and ‘Price’ as ‘Order By’ then it filters the results as expected, both with ‘Order By’ and with the search term.
If you then change the ‘Order By’ to another option such as ‘Alphabetical’ and then back to ‘Price’, then the AJAX results don’t take into account the search terms. Not sure if I’ve just made a stupid mistake.
function ch_member_order_options() {
?>
<option value="price"><?php _e( 'Price', 'buddypress' ); ?></option>
<?php
}
add_action( 'bp_members_directory_order_options', 'ch_member_order_options' );
// filter ajax members by contributing
function ch_filter_ajax_querystring( $querystring = '', $object = '' ) {
if( $object != 'members' )
return $querystring;
$defaults = array(
'type' => 'active',
'action' => 'active',
'scope' => 'all',
'page' => 1,
'user_id' => 0,
'search_terms' => '',
'exclude' => false,
);
$ch_querystring = wp_parse_args( $querystring, $defaults );
if( $ch_querystring['type'] == 'price' ) {
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/wp-load.php";
require_once($path);
global $wpdb;
$field_id = 5;
$user_query = $wpdb->get_col("SELECT object_id FROM " . $wpdb->prefix . "term_relationships WHERE term_taxonomy_id = " . $field_id);
$users_str = '';
foreach ( $user_query as $user ) {
$users_str .= $user . ',';
}
$users_str = rtrim($users_str, ",");
$user_ord = $wpdb->get_col("SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE user_id in (" . $users_str . ") AND field_id=18 ORDER BY value ASC");
$user_ordf = '';
foreach ($user_ord as $user_o) {
$user_ordf .= $user_o . ',';
}
$user_ordf = rtrim($user_ordf,",");
$ch_querystring['include'] = $user_ordf;
$ch_querystring['type'] = 'alphabetical';
$search_terms = $_GET['s'];
if(!empty($search_terms)) {
$ch_querystring['search_terms'] = $search_terms;
}
//var_dump($ch_querystring);
return $ch_querystring;
}
else
return $querystring;
}
add_filter( 'bp_ajax_querystring', 'ch_filter_ajax_querystring', 20, 2 );