WordPress.org

Make WordPress Core

Ticket #14118: get_users_by_metadata.14118.diff

File get_users_by_metadata.14118.diff, 1.1 KB (added by filosofo, 4 years ago)
  • wp-includes/user.php

     
    225225} 
    226226 
    227227/** 
     228 * Retreive user IDs from usermeta values. 
     229 * 
     230 * @param string $meta_key The meta key 
     231 * @param string $meta_value The meta value 
     232 * @return array The user IDs that have those meta-key and meta-value pairs. 
     233 */ 
     234function get_users_by_metadata( $meta_key = '' , $meta_value = '' ) { 
     235        global $wpdb; 
     236        $users = array(); 
     237 
     238        if ( empty( $meta_key ) ) 
     239                return $users; 
     240         
     241        if ( empty( $meta_value ) ) 
     242                $users = (array) $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ) ); 
     243        else 
     244                $users = (array) $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) ); 
     245         
     246        return $users; 
     247} 
     248 
     249/** 
    228250 * Retrieve user option that can be either per Site or per Network. 
    229251 * 
    230252 * If the user ID is not given, then the current user will be used instead. If