Opened 11 years ago
Closed 10 years ago
#5659 closed defect (bug)
When querying users with usermeta, if none are found, `bp_core_get_users` returns all users
Reported by: | needle | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Members | Keywords: | good-first-bug close |
Cc: |
Description
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.
Change History (7)
#1
@
11 years ago
- Keywords needs-refresh needs-unit-tests added
- Milestone changed from Awaiting Review to 2.1
#2
@
10 years ago
- Keywords needs-patch good-first-bug added; needs-refresh removed
This would be pretty nice to fix for 2.1 as it seems an egregious bug. The original reporter and Boone describe the best way to fix this ticket, so the remaining work is to create a patch, and write some unit tests.
This should be a good way into learning how to write BuddyPress unit tests for contributors who have created one or two BuddyPress fixes before.
#3
@
10 years ago
- Keywords early added
- Milestone changed from 2.1 to 2.2
- Owner set to DJPaul
- Status changed from new to assigned
Thanks for the report. Your solution is pretty close to what is needed (we generally use the
$no_results['where']
clause instead, which speeds up the query; see eg the way 'user_id' is parsed in BP_User_Query). If you'd like to write a BP patch using this technique, please do.Unit tests are also needed to demonstrate this behavior.