WordPress.org

Make WordPress Core

Opened 15 months ago

Closed 6 months ago

#23228 closed defect (bug) (invalid)

get_users() cannot include AND exclude

Reported by: 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)

comment:1 follow-up: knutsp15 months ago

Use case?

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

comment:2 scribu15 months ago

  • Keywords reporter-feedback added

comment:3 in reply to: ↑ 1 bananastalktome15 months 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().

comment:4 SergeyBiryukov15 months ago

  • Keywords needs-codex added

comment:6 jnhghy6 months 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?

comment:7 helen6 months 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.