Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#23228 closed defect (bug) (invalid)

get_users() cannot include AND exclude

Reported by: dlim_vernier's profile dlim_vernier Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5
Component: Users Keywords: reporter-feedback
Focuses: Cc:

Description

The codex describes the parameters for get_users() as an array of options, so it seems like you can set an array of includes and an array of excludes. But the code looks like it can only do one or the other. It would be a simple fix.

Lines 521-527 in wp-includes/user.php

if ( !empty( $qv['include'] ) ) {
	$ids = implode( ',', wp_parse_id_list( $qv['include'] ) );
	$this->query_where .= " AND $wpdb->users.ID IN ($ids)";
} elseif ( !empty($qv['exclude']) ) {
	$ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
	$this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
}

To fix, change the elseif to its own if:

if ( !empty( $qv['include'] ) ) {
	$ids = implode( ',', wp_parse_id_list( $qv['include'] ) );
	$this->query_where .= " AND $wpdb->users.ID IN ($ids)";
}

if ( !empty($qv['exclude']) ) {
	$ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
	$this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
}

Thanks for the consideration.

Change History (7)

#1 follow-up: @knutsp
11 years ago

Use case?

Otherwise, update Codex to reflect that when include is used, there is no exclude.

#2 @scribu
11 years ago

  • Keywords reporter-feedback added

#3 in reply to: ↑ 1 @bananastalktome
11 years ago

Replying to knutsp:

Otherwise, update Codex to reflect that when include is used, there is no exclude.

Just as a note, if updating the Codex to note that include & exclude are mutually exclusive,
http://codex.wordpress.org/Function_Reference/wp_dropdown_users should be updated as well since it takes include & exclude args which are passed to get_users().

#4 @SergeyBiryukov
11 years ago

  • Keywords needs-codex added

#6 @jnhghy
11 years ago

  • Cc jnhghy added

would adding a parameter to solve the priority be a solution? like 'priority'=>'include' or 'priority'=>'exclude' with a default for any of them?

#7 @helen
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

I'm closing this - I don't see why you would expect include and exclude to work at the same time in a single query, and the Codex has been updated. Feel free to reopen with a concrete example of why this is necessary in a core API.

Note: See TracTickets for help on using tickets.