#36624 closed enhancement (fixed)
Add nicename__in and nicename__not_in to WP_User_Query
| Reported by: | johnjamesjacoby | Owned by: | boonebgorges |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.7 |
| Component: | Users | Version: | |
| Severity: | normal | Keywords: | has-unit-tests has-patch |
| Cc: | Focuses: |
Description
I have a need to query for multiple users by their nicenames. Right now, I do something like this:
// Get usernames
$usernames = some_function_to_get_usernames();
// Loop through usernames and link to profiles
foreach ( (array) $usernames as $username ) {
// Skip if username does not exist or user is not active
$user = get_user_by( 'slug', $username );
if ( empty( $user->ID ) ) {
continue;
}
// Some things...
}
It would be great if WP_User_Query worked in the following ways:
__in:
$users = new WP_User_Query( array( 'nicename__in' => array( 'admin', 'john', 'paul' ) ) );
__not_in:
$users = new WP_User_Query( array( 'nicename__not_in' => array( 'admin' ) ) );
The "nice" thing about this, is user_nicename is an indexed column, so these queries should be speedy and internally optimized by MySQL.
Attachments (2)
Change History (15)
#2
follow-up:
↓ 3
@
10 years ago
- Keywords good-first-bug needs-unit-tests added; 2nd-opinion removed
- Milestone Awaiting Review → Future Release
If we're going to add nicename__*, it probably makes sense to do login__* at the same time.
#3
in reply to: ↑ 2
;
follow-up:
↓ 5
@
10 years ago
Replying to boonebgorges:
If we're going to add
nicename__*, it probably makes sense to dologin__*at the same time.
Sure thing. I can work on a patch.
#5
in reply to: ↑ 3
@
10 years ago
Replying to johnjamesjacoby:
Replying to boonebgorges:
If we're going to add
nicename__*, it probably makes sense to dologin__*at the same time.
Sure thing. I can work on a patch.
@johnjamesjacoby - you still working on this?
#6
follow-up:
↓ 7
@
10 years ago
@atomicadam I have yet to, so don't let that stop you from taking a stab at it. It's mostly a copy-paste job from existing (don't forget the orderby additions.)
#7
in reply to: ↑ 6
@
10 years ago
Replying to johnjamesjacoby:
@atomicadam I have yet to, so don't let that stop you from taking a stab at it. It's mostly a copy-paste job from existing (don't forget the
orderbyadditions.)
sounds good, i'll see what I can come up with. thanks
#8
@
10 years ago
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Took a stab at it. This is my first patch with real code and unit tests, so let me know if I missed something.
#9
@
10 years ago
- Keywords needs-patch added; good-first-bug has-patch removed
- Milestone Future Release → 4.7
- Owner changed from to
Took a stab at it. This is my first patch with real code and unit tests, so let me know if I missed something.
Wow, this is a great first patch! Thanks, @ryanplas! A few small things:
- We'll need a
@sinceannotation for the newWP_User_Query::prepare_query()parameters. - "Matched users must have at least one..." is a weird thing to say about logins or nicenames, since users can only have one :) Maybe find a better way to phrase it.
- The
__inand__not_inclauses need to have their values sanitized. Something like this should work:array_map( 'esc_sql', $qv['nicename__in'] )...
#10
@
10 years ago
- Keywords has-patch added; needs-patch removed
Cleaned up the wording in the docs, added the @since annotation and added sanitization.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I like this, great idea :)