Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Filter users by meta in BP Members Directory

By: imath on Jun 25th, 2012  |  syntax: PHP  |  size: 3.52 KB  |  views: 185  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3. * as i test on bp members directory
  4. * i first add a new option in member filter
  5. * it could be the score user meta_key in your case
  6. * in mine it's the latest geo position user meta_key (bpci_latest_address)
  7. */
  8. add_action( 'bp_members_directory_order_options', 'bp_imath_experimental_add_umeta_in_filter_list');
  9.  
  10. function bp_imath_experimental_add_umeta_in_filter_list() {
  11.         ?>
  12.         <option value="bpci_latest_address"><?php _e( 'Latest Geo Postion', 'buddypress' ); ?></option>
  13.         <?php
  14. }
  15.  
  16.  
  17. /**
  18. * the main trick is to directly apply a filter on the request
  19. * that BP_Core_User::get_users will play
  20. */
  21. add_filter('bp_core_get_paged_users_sql', 'bp_imath_experimental_umeta_filter_select', 10, 2);
  22.  
  23. function bp_imath_experimental_umeta_filter_select($query, $sql) {
  24.        
  25.         /* check the meta before modifying the query...*/
  26.         if( !empty($sql['where_meta']) && strpos( $sql['where_meta'], 'bpci_latest_address') >= 0 ) {
  27.                 /**
  28.                 * by default the ORDER BY is on um.meta_value
  29.                 * but our meta_value is umm.meta_value, so we need to change this!
  30.                 * you can also change the order (DESC or ASC)
  31.                 */
  32.                 $sql[0] = 'ORDER BY umm.meta_value DESC';
  33.                 $sql['select_meta'] = ', umm.meta_value as position';
  34.                 $query = join( ' ', (array)$sql );     
  35.         }
  36.        
  37.         return $query;
  38.        
  39. }
  40.  
  41.  
  42. /**
  43. * we must also filter the count request for pagination
  44. * that BP_Core_User::get_users will play
  45. */
  46. add_filter('bp_core_get_total_users_sql', 'bp_imath_experimental_umeta_filter_count', 10, 2);
  47.  
  48. function bp_imath_experimental_umeta_filter_count($query, $sql) {
  49.        
  50.         /* check the meta before modifying the query...*/
  51.         if( !empty($sql['where_meta']) && strpos( $sql['where_meta'], 'bpci_latest_address') >= 0 ) {
  52.                 // you can change order by pref (DESC or ASC)
  53.                 $sql[1] = 'ORDER BY umm.meta_value DESC';
  54.                 $sql['select_meta'] = '';
  55.                 $query = join( ' ', (array)$sql );     
  56.         }
  57.        
  58.         return $query;
  59. }
  60.  
  61. /**
  62. * as i play directly on the members directory
  63. * i need to filter the ajax querystring to add the meta_key argument
  64. * meta_key=bpci_latest_address
  65. */
  66. add_filter( 'bp_dtheme_ajax_querystring', 'bp_imath_experimental_dtheme_ajax_query', 10, 7 );
  67.  
  68. function bp_imath_experimental_dtheme_ajax_query( $query_string, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras ){
  69.        
  70.         /* check filter before modifying querystring...*/
  71.         if( $object_filter == 'bpci_latest_address' ) {
  72.                
  73.                 if( strpos($query_string, 'type=bpci_latest_address&action=bpci_latest_address') >= 0 )
  74.                         $query_string = str_replace('type=bpci_latest_address&action=bpci_latest_address', 'meta_key=bpci_latest_address', $query_string);
  75.                
  76.         }
  77.                
  78.         return $query_string;
  79.                
  80. }
  81.  
  82. /**
  83. * now as i added 'umm.meta_value as position'
  84. * in the select query, let's use this value
  85. * instead of using echo get_user_meta( $user_id ,'bpci_latest_address', true)
  86. */
  87. function bp_imath_experimental_the_member_position() {
  88.         echo bp_imath_experimental_get_the_member_position();
  89. }
  90.         function bp_imath_experimental_get_the_member_position(){
  91.                 global $members_template;
  92.                
  93.                 return apply_filters('bp_imath_experimental_get_the_member_position', $members_template->member->position);
  94.         }
  95.        
  96. /**
  97. * finally let's display the position or score in your case..
  98. * by using the hook just after the latest activity in the members-loop template
  99. */
  100. add_action('bp_directory_members_item', 'bp_imath_experimental_finally_display');
  101.  
  102. function bp_imath_experimental_finally_display(){
  103.         ?>
  104.         <div class="whatever"><?php bp_imath_experimental_the_member_position();?></div>
  105.         <?php
  106. }
  107. ?>