Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_has_members meta query filter


  • rocketmena
    Participant

    @rocketmena

    how to add a meta key filtering in default function bp_has_members( $args = ” ) . Now filtering members based on name or user name only. I want to filter them based on position also.if we enter name or position, filter need to work.In default code, meta_key, meta_value set as false. Please assist.

    function bp_has_members( $args = ” ) {
    global $members_template;

    // Default user ID.
    $user_id = 0;

    // User filtering.
    if ( bp_is_user_friends() && ! bp_is_user_friend_requests() ) {
    $user_id = bp_displayed_user_id();
    }

    $member_type = bp_get_current_member_type();
    if ( ! $member_type && ! empty( $_GET[‘member_type’] ) ) {
    if ( is_array( $_GET[‘member_type’] ) ) {
    $member_type = $_GET[‘member_type’];
    } else {
    // Can be a comma-separated list.
    $member_type = explode( ‘,’, $_GET[‘member_type’] );
    }
    }

    $search_terms_default = null;
    $search_query_arg = bp_core_get_component_search_query_arg( ‘members’ );
    if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
    $search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
    }

    // Type: active ( default ) | random | newest | popular | online | alphabetical.
    $r = bp_parse_args( $args, array(
    ‘type’ => ‘active’,
    ‘page’ => 1,
    ‘per_page’ => 20,
    ‘max’ => false,

    ‘page_arg’ => ‘upage’, // See https://buddypress.trac.wordpress.org/ticket/3679.

    ‘include’ => false, // Pass a user_id or a list (comma-separated or array) of user_ids to only show these users.
    ‘exclude’ => false, // Pass a user_id or a list (comma-separated or array) of user_ids to exclude these users.

    ‘user_id’ => $user_id, // Pass a user_id to only show friends of this user.
    ‘member_type’ => $member_type,
    ‘member_type__in’ => ”,
    ‘member_type__not_in’ => ”,
    ‘search_terms’ => $search_terms_default,

    ‘meta_key’ => false, // Only return users with this usermeta.
    ‘meta_value’ => false, // Only return users where the usermeta value matches. Requires meta_key.

    ‘populate_extras’ => true // Fetch usermeta? Friend count, last active etc.
    ), ‘has_members’ );

    // Pass a filter if ?s= is set.
    if ( is_null( $r[‘search_terms’] ) ) {
    if ( !empty( $_REQUEST[‘s’] ) ) {
    $r[‘search_terms’] = $_REQUEST[‘s’];
    } else {
    $r[‘search_terms’] = false;
    }
    }

    // Set per_page to max if max is larger than per_page.
    if ( !empty( $r[‘max’] ) && ( $r[‘per_page’] > $r[‘max’] ) ) {
    $r[‘per_page’] = $r[‘max’];
    }

    // Query for members and populate $members_template global.
    $members_template = new BP_Core_Members_Template(
    $r[‘type’],
    $r[‘page’],
    $r[‘per_page’],
    $r[‘max’],
    $r[‘user_id’],
    $r[‘search_terms’],
    $r[‘include’],
    $r[‘populate_extras’],
    $r[‘exclude’],
    $r[‘meta_key’],
    $r[‘meta_value’],
    $r[‘page_arg’],
    $r[‘member_type’],
    $r[‘member_type__in’],
    $r[‘member_type__not_in’]
    );

    /**
    * Filters whether or not BuddyPress has members to iterate over.
    *
    * @since 1.2.4
    * @since 2.6.0 Added the $r parameter
    *
    * @param bool $value Whether or not there are members to iterate over.
    * @param array $members_template Populated $members_template global.
    * @param array $r Array of arguments passed into the BP_Core_Members_Template class.
    */
    return apply_filters( ‘bp_has_members’, $members_template->has_members(), $members_template, $r );
    }

  • You must be logged in to reply to this topic.
Skip to toolbar