#36624 closed enhancement (fixed)
Add nicename__in and nicename__not_in to WP_User_Query
Reported by: | johnjamesjacoby | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Users | Keywords: | has-unit-tests has-patch |
Focuses: | Cc: |
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
@
8 years ago
- Keywords good-first-bug needs-unit-tests added; 2nd-opinion removed
- Milestone changed from Awaiting Review to 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
@
8 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
@
8 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
@
8 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
@
8 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
orderby
additions.)
sounds good, i'll see what I can come up with. thanks
#8
@
8 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
@
8 years ago
- Keywords needs-patch added; good-first-bug has-patch removed
- Milestone changed from Future Release to 4.7
- Owner changed from johnjamesjacoby to ryanplas
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
@since
annotation 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
__in
and__not_in
clauses need to have their values sanitized. Something like this should work:array_map( 'esc_sql', $qv['nicename__in'] )
...
#10
@
8 years ago
- Keywords has-patch added; needs-patch removed
Cleaned up the wording in the docs, added the since annotation and added sanitization.
I like this, great idea :)