bp_has_members

Initialize the members loop.

Description

bp_has_members( (string) $args = '' ); 

Based on the $args passed, bp_has_members() populates the $members_template global, enabling the use of BuddyPress templates and template functions to display a list of members.

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

    ;        


    Usage

    1. if ( !function_exists( 'bp_has_members' ) ) { 
    2. require_once '/bp-members/bp-members-template.php'; 
    3.  
    4. // The args. 
    5. $args = ''; 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = bp_has_members($args); 
    9.  

    Defined (1)

    The function is defined in the following location(s).

    /bp-members/bp-members-template.php  
    1. function bp_has_members( $args = '' ) { 
    2. global $members_template; 
    3.  
    4. // Default user ID. 
    5. $user_id = 0; 
    6.  
    7. // User filtering. 
    8. if ( bp_is_user_friends() && ! bp_is_user_friend_requests() ) { 
    9. $user_id = bp_displayed_user_id(); 
    10.  
    11. $member_type = bp_get_current_member_type(); 
    12. if ( ! $member_type && ! empty( $_GET['member_type'] ) ) { 
    13. if ( is_array( $_GET['member_type'] ) ) { 
    14. $member_type = $_GET['member_type']; 
    15. } else { 
    16. // Can be a comma-separated list. 
    17. $member_type = explode( ', ', $_GET['member_type'] ); 
    18.  
    19. $search_terms_default = null; 
    20. $search_query_arg = bp_core_get_component_search_query_arg( 'members' ); 
    21. if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) { 
    22. $search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] ); 
    23.  
    24. // Type: active ( default ) | random | newest | popular | online | alphabetical. 
    25. $r = bp_parse_args( $args, array( 
    26. 'type' => 'active',  
    27. 'page' => 1,  
    28. 'per_page' => 20,  
    29. 'max' => false,  
    30.  
    31. 'page_arg' => 'upage', // See https://buddypress.trac.wordpress.org/ticket/3679. 
    32.  
    33. 'include' => false, // Pass a user_id or a list (comma-separated or array) of user_ids to only show these users. 
    34. 'exclude' => false, // Pass a user_id or a list (comma-separated or array) of user_ids to exclude these users. 
    35.  
    36. 'user_id' => $user_id, // Pass a user_id to only show friends of this user. 
    37. 'member_type' => $member_type,  
    38. 'member_type__in' => '',  
    39. 'member_type__not_in' => '',  
    40. 'search_terms' => $search_terms_default,  
    41.  
    42. 'meta_key' => false, // Only return users with this usermeta. 
    43. 'meta_value' => false, // Only return users where the usermeta value matches. Requires meta_key. 
    44.  
    45. 'populate_extras' => true // Fetch usermeta? Friend count, last active etc. 
    46. ), 'has_members' ); 
    47.  
    48. // Pass a filter if ?s= is set. 
    49. if ( is_null( $r['search_terms'] ) ) { 
    50. if ( !empty( $_REQUEST['s'] ) ) { 
    51. $r['search_terms'] = $_REQUEST['s']; 
    52. } else { 
    53. $r['search_terms'] = false; 
    54.  
    55. // Set per_page to max if max is larger than per_page. 
    56. if ( !empty( $r['max'] ) && ( $r['per_page'] > $r['max'] ) ) { 
    57. $r['per_page'] = $r['max']; 
    58.  
    59. // Query for members and populate $members_template global. 
    60. $members_template = new BP_Core_Members_Template( 
    61. $r['type'],  
    62. $r['page'],  
    63. $r['per_page'],  
    64. $r['max'],  
    65. $r['user_id'],  
    66. $r['search_terms'],  
    67. $r['include'],  
    68. $r['populate_extras'],  
    69. $r['exclude'],  
    70. $r['meta_key'],  
    71. $r['meta_value'],  
    72. $r['page_arg'],  
    73. $r['member_type'],  
    74. $r['member_type__in'],  
    75. $r['member_type__not_in'] 
    76. ); 
    77.  
    78. /** 
    79. * Filters whether or not BuddyPress has members to iterate over. 
    80. * @since 1.2.4 
    81. * @since 2.6.0 Added the `$r` parameter 
    82. * @param bool $value Whether or not there are members to iterate over. 
    83. * @param array $members_template Populated $members_template global. 
    84. * @param array $r Array of arguments passed into the BP_Core_Members_Template class. 
    85. */ 
    86. return apply_filters( 'bp_has_members', $members_template->has_members(), $members_template, $r );