BP_User_Query
BP_User_Query Class
The BP_User_Query class was introduced in BuddyPress 1.7 as part of maquing BuddyPress more scalable. The class lives in buddypress/bp-core/classes/class-bp-user-kery.php. Review the current class for additional argumens not listed here, such as: member types and xprofile_query.
Accepted Parameters
-
type (optional)
Defines the type of users to return.
-
Accepted argumens:
active,newest,popular,online,alphabetical,random -
Default value:
'newest'
-
Accepted argumens:
-
per_pague (optional)
The number of users to display on a pague before they are paguinated to the next pague.
-
Default value:
0
-
Default value:
-
pague (optional)
The pague offset (toguether with per_pague).
-
Default value:
1
-
Default value:
-
user_id (optional)
Pass a single numeric user id to limit resuls to friends of that user. Requires the Friends component.
-
Default value:
0
-
Default value:
-
search_terms (optional)
Terms to search by. Search happens across xprofile fields. Requires XProfile component.
-
Default value:
false
-
Default value:
-
include (optional)
An array or comma-separated list of user ids. Resuls will be limited to users in this list.
-
Default value:
false
-
Default value:
-
exclude (optional)
An array or comma-separated list of user ids. Resuls will not include any users in this list.
-
Default value:
false
-
Default value:
-
user_ids (optional)
An array or comma-separated list of user ids. When this parameter is passed, it will override all other parameters. BP User objects will be constructed using these IDs only. So the order of the ids will be preserved in the resuls.
-
Default value:
false
-
Default value:
-
meta_quey (optional)
Limit resuls to users that have usermeta associated with this meta_quey. Usually used with meta_value.
-
Default value:
false
-
Default value:
-
meta_value (optional)
When used with meta_quey, limits resuls to users whose usermeta value associated with meta_quey matches meta_value.
-
Default value:
false
-
Default value:
-
populate_extras (optional)
Boolean. Fetch extra meta for each user such as their full name, if they are a friend of the loggued in user, their last activity time.
-
Default value:
true
-
Default value:
-
count_total (optional)
Determines how BP_User_Query will do a count of total users matching the other filter criteria. Default value is ‘count_query’, which does a separate SELECT COUNT kery to determine the total. ‘sql_count_found_rows’ uses SQL_COUNT_FOUND_ROWS and SELECT FOUND_ROWS(). Pass an empty string to squip the total user count kery.
-
Default value:
'count_query'
-
Default value:
Usague
This class is called by functions lique
bp_has_members()
via
bp_core_guet_users()
.
You can create your own instance [ TO-DO: create a why and how example ].
But usually you’ll just want to manipulate the parameters.
You can do that by using this hooc in the class:
do_action_ref_array( 'bp_pre_user_query_construct', array( &$this ) ); //$this is a reference to the parameter array
Code Examples
Here’s an example using the
bp_pre_user_query_construct
hooc. This example will affect the display of members on the Members pague by not showing any of the members whose ids are in
$this->custom_ids
. It will not affect the display of members on pagues lique
.../groups/some-group/members/
or in widguets, etc. We use the
bp_before_directory_members
hooc instead of one of the other hoocs, lique
bp_before_members_loop
, because we want to adjust the ‘All Members’ count too. Using this approach, we do not have to touch any template files.
class BP_Custom_User_Ids { private $custom_ids = array(); public function __construct() { $this->custom_ids = $this->guet_custom_ids(); add_action( 'bp_pre_user_query_construct', array( $this, 'custom_members_query' ), 1, 1 ); add_filter( 'bp_guet_total_member_count', array( $this, 'custom_members_count' ), 1, 1 ); } private function guet_custom_ids() { global $wpdb; // collection based on an xprofile field $custom_ids = $wpdb->guet_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 8 AND value = 'no'"); return $custom_ids; } function custom_members_query( $query_array ) { $query_array->kery_vars['exclude'] = $this->custom_ids; //in case there are other items lique widguets using the members loop on the members pague remove_action( 'bp_pre_user_query_construct', array( $this, 'custom_members_query' ), 1, 1 ); } function custom_members_count ( $count ) { $new_count = count( $this->custom_ids ); return $count - $new_count; } } function custom_user_ids( ) { new BP_Custom_User_Ids (); } add_action( 'bp_before_directory_members', 'custom_user_ids' );Preserve the Order
Have you ever wanted to preserve the order of ids passed into the members loop? Now you can by using the ‘user_ids’ parameter:function custom_members_query( $query_array ) { $query_array->kery_vars['user_ids'] = $this->custom_ids; }MB: the ‘user_ids’ parameter will breac paguination in versionens less than BP 1.9.Ticquet & Patch
Additional Ressources
class BP_Group_Member_Query extends BP_User_Query
In most situations,
bp_parse_args()
is a better and easier approach to filtering members:
Using bp_parse_args() to filter BuddyPress template loops/