Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Adding new ‘Order By’ on members loop

@ch1n3s3b0y

Participant

@shanebp So I have sort of made progress. I found this post by @eberger3. He seemed to be trying to do something similar to me. With his code I have now managed to order the results by price, which is great, but I am also experiencing the same issues he had:

  • The pagination doesn’t work
  • The member count shows ‘Viewing 1 member’ when there are many more
  • The “search members” box doesn’t work. It returns the entire directory

Is this because I am using bp_user_query and not bp_ajax_querystring?

<?php
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' );
add_action( 'bp_pre_user_query', 'sort_by_price' );
function sort_by_price( $bp_user_query ) {
	// Only run this if one of our custom options is selected
	if ( in_array( $bp_user_query->query_vars['type'], array( 'price') ) ) {
		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, ",");

		// Adjust SELECT
		$bp_user_query->uid_clauses['select'] = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data";

		// Adjust WHERE
		$bp_user_query->uid_clauses['where'] = "WHERE user_id in (" . $users_str . ") AND field_id=18";

		// Adjust ORDER BY
	    $bp_user_query->uid_clauses['orderby'] = "ORDER BY CAST(value as UNSIGNED) ASC";
		
		$bp_user_query->uid_clauses['per_page'] = 10;
	}
}
?>

Edit: I can see this thread is in Installation category. This was a mistake. I should have but it into ‘How To’ or ‘Creating and Extending’ I can’t change this though.

Skip to toolbar