Skip to:

Opened 10 years ago

Closed 10 years ago

#5904 closed defect (bug) (fixed)

No join to user meta table if key doesn't exist yet.

Reported by: timfield's profile timfield Owned by: imath's profile imath
Milestone: 2.2 Priority: normal
Severity: normal Version: 2.1
Component: Core Keywords: has-patch commit


I want to filter all members by a meta key.


    if(!isset($args['scope']) || $args['scope']=='all')
    return $args;

This works, but only if at least one member has that meta key. I would expect this to always cause a join to the user meta. If no users have this meta key then none should show up.

The problem is here "public/wp-content/plugins/buddypress/bp-core/bp-core-classes.php"

// 'meta_key', 'meta_value' allow usermeta search
// To avoid global joins, do a separate query
if ( false !== $meta_key ) {
        $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key );

        if ( false !== $meta_value ) {
$meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value );

        $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 ) ) . ")";

Attachments (2)

5904.patch (741 bytes) - added by imath 10 years ago.
5904.unittests.patch (1.4 KB) - added by imath 10 years ago.

Download all attachments as: .zip

Change History (7)

#1 @imath
10 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 2.2

Well i guess we could force the query to fail..

10 years ago

#2 @boonebgorges
10 years ago

  • Keywords needs-unit-tests added; dev-feedback removed

5904.patch seems like a reasonable solution. Needs unit test, please.

#3 @imath
10 years ago

5904.unittests.patch adds two unit tests :

  • 1 to check if querying with user meta arguments is ok
  • the other to check results are empty if no user has the user meta

#4 @boonebgorges
10 years ago

  • Keywords commit added; needs-unit-tests removed

Looks good to me. Thanks, imath.

#5 @imath
10 years ago

  • Owner set to imath
  • Resolution set to fixed
  • Status changed from new to closed

In 9051:

When using BP_User_Query with meta_key and meta_value arguments, make sure no results are displayed if no user match the meta arguments.

Fixes #5904

Note: See TracTickets for help on using tickets.