[buddypress-trac] [BuddyPress Trac] #5659: When querying users with usermeta, if none are found, `bp_core_get_users` returns all users

buddypress-trac noreply at wordpress.org
Fri May 23 13:26:55 UTC 2014


#5659: When querying users with usermeta, if none are found, `bp_core_get_users`
returns all users
--------------------------+-----------------------------
 Reporter:  needle        |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Members       |    Version:
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 If I perform a query for users with a particular meta_key/meta_value
 combination via `bp_core_get_users()`,
 `BP_User_Query->prepare_user_ids_query()` constructs a query which returns
 ''all users''. This does not seem to me to be expected behaviour - I would
 expect ''no users'' to be returned.

 The source of the issue begins at line 380 of `bp-core-classes.php`:

 {{{
 $found_user_ids = $wpdb->get_col( $meta_sql );

 if ( ! empty( $found_user_ids ) ) {
         $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',',
 wp_parse_id_list( $found_user_ids ) ) . ")";
 }
 }}}

 If no users are found, then the query does not restrict by `user_id` at
 all. Surely there needs to be an amendment to the query if
 `$found_user_ids` is empty. Something simple like this might suffice:

 {{{
 $found_user_ids = $wpdb->get_col( $meta_sql );

 if ( ! empty( $found_user_ids ) ) {
         $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',',
 wp_parse_id_list( $found_user_ids ) ) . ")";
 } else {
         $sql['where'][] = "u.{$this->uid_name} = '0'";
 }
 }}}

 This has the desired effect, but is probably inefficient. I defer to the
 SQL experts here to write something more elegant.

--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/5659>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list