Skip to:
Content

BuddyPress.org

Changeset 4360


Ignore:
Timestamp:
05/15/2011 03:01:28 PM (13 years ago)
Author:
boonebgorges
Message:

Adds meta_key and meta_value params to bp_has_members() and related functions. Fixes #3205

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-classes.php

    r4330 r4360  
    110110    /** Static Functions ******************************************************/
    111111
    112     function get_users( $type, $limit = 0, $page = 1, $user_id = 0, $include = false, $search_terms = false, $populate_extras = true, $exclude = false ) {
     112    function get_users( $type, $limit = 0, $page = 1, $user_id = 0, $include = false, $search_terms = false, $populate_extras = true, $exclude = false, $meta_key = false, $meta_value = false ) {
    113113        global $wpdb, $bp;
    114114
     
    125125        if ( 'alphabetical' == $type )
    126126            $sql['select_alpha'] = ", pd.value as fullname";
     127       
     128        if ( $meta_key ) {
     129            $sql['select_meta'] = ", umm.meta_key";
     130           
     131            if ( $meta_value )
     132                $sql['select_meta'] .= ", umm.meta_value";
     133        }
    127134
    128135        $sql['from'] = "FROM " . CUSTOM_USER_TABLE . " u LEFT JOIN " . CUSTOM_USER_META_TABLE . " um ON um.user_id = u.ID";
     
    130137        if ( $search_terms && bp_is_active( 'xprofile' ) || 'alphabetical' == $type )
    131138            $sql['join_profiledata'] = "LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id";
     139           
     140        if ( $meta_key )
     141            $sql['join_meta'] = "LEFT JOIN {$wpdb->usermeta} umm ON umm.user_id = u.ID";
    132142
    133143        $sql['where'] = 'WHERE ' . bp_core_get_status_sql( 'u.' );
     
    174184            $search_terms             = like_escape( $wpdb->escape( $search_terms ) );
    175185            $sql['where_searchterms'] = "AND pd.value LIKE '%%$search_terms%%'";
     186        }
     187       
     188        if ( $meta_key ) {
     189            $sql['where_meta'] = $wpdb->prepare( " AND umm.meta_key = %s", $meta_key );
     190           
     191            // If a meta value is provided, match it
     192            if ( $meta_value ) {
     193                $sql['where_meta'] .= $wpdb->prepare( " AND umm.meta_value = %s", $meta_value );
     194            }
    176195        }
    177196
  • trunk/bp-members/bp-members-functions.php

    r4088 r4360  
    6161        'exclude'         => false,    // Users to exclude from results
    6262        'search_terms'    => false,    // Limit to users that match these search terms
     63        'meta_key'    => false,    // Limit to users who have this piece of usermeta
     64        'meta_value'      => false,    // With meta_key, limit to users where usermeta matches this value
    6365
    6466        'include'         => false,    // Pass comma separated list of user_ids to limit to only these users
     
    7173    extract( $params, EXTR_SKIP );
    7274
    73     return apply_filters( 'bp_core_get_users', BP_Core_User::get_users( $type, $per_page, $page, $user_id, $include, $search_terms, $populate_extras, $exclude ), $params );
     75    return apply_filters( 'bp_core_get_users', BP_Core_User::get_users( $type, $per_page, $page, $user_id, $include, $search_terms, $populate_extras, $exclude, $meta_key, $meta_value ), $params );
    7476}
    7577
  • trunk/bp-members/bp-members-template.php

    r4330 r4360  
    140140    var $total_member_count;
    141141
    142     function bp_core_members_template( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude ) {
    143         $this->__construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude );
    144     }
    145 
    146     function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude ) {
     142    function bp_core_members_template( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value ) {
     143        $this->__construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value );
     144    }
     145
     146    function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value ) {
    147147        global $bp;
    148148       
     
    156156            $this->members = BP_Core_User::get_specific_users( $include, $this->pag_num, $this->pag_page, $populate_extras );
    157157        else
    158             $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude ) );
     158            $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude, 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );
    159159
    160160        if ( !$max || $max >= (int)$this->members['total'] )
     
    268268        'user_id'         => $user_id,      // Pass a user_id to only show friends of this user
    269269        'search_terms'    => $search_terms, // Pass search_terms to filter users by their profile data
     270       
     271        'meta_key'    => false,     // Only return users with this usermeta
     272        'meta_value'      => false,     // Only return users where the usermeta value matches. Requires meta_key
    270273
    271274        'populate_extras' => true           // Fetch usermeta? Friend count, last active etc.
     
    291294        return false;
    292295
    293     $members_template = new BP_Core_Members_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $include, (bool)$populate_extras, $exclude );
     296    $members_template = new BP_Core_Members_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $include, (bool)$populate_extras, $exclude, $meta_key, $meta_value );
    294297    return apply_filters( 'bp_has_members', $members_template->has_members(), $members_template );
    295298}
Note: See TracChangeset for help on using the changeset viewer.