Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#36624 closed enhancement (fixed)

Add nicename__in and nicename__not_in to WP_User_Query

Reported by: johnjamesjacoby's profile johnjamesjacoby Owned by: boonebgorges's profile 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)

36624.patch (12.0 KB) - added by ryanplas 8 years ago.
36624.2.patch (12.7 KB) - added by ryanplas 8 years ago.

Download all attachments as: .zip

Change History (15)

#1 @sebastian.pisula
8 years ago

I like this, great idea :)

#2 follow-up: @boonebgorges
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: @johnjamesjacoby
8 years ago

Replying to boonebgorges:

If we're going to add nicename__*, it probably makes sense to do login__* at the same time.

Sure thing. I can refresh the patch.

Version 0, edited 8 years ago by johnjamesjacoby (next)

#4 @DrewAPicture
8 years ago

  • Owner set to johnjamesjacoby
  • Status changed from new to assigned

#5 in reply to: ↑ 3 @atomicadam
8 years ago

Replying to johnjamesjacoby:

Replying to boonebgorges:

If we're going to add nicename__*, it probably makes sense to do login__* at the same time.

Sure thing. I can work on a patch.

@johnjamesjacoby - you still working on this?

#6 follow-up: @johnjamesjacoby
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 @atomicadam
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

@ryanplas
8 years ago

#8 @ryanplas
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 @boonebgorges
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 new WP_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'] ) ...

@ryanplas
8 years ago

#10 @ryanplas
8 years ago

  • Keywords has-patch added; needs-patch removed

Cleaned up the wording in the docs, added the @since annotation and added sanitization.

Last edited 8 years ago by ryanplas (previous) (diff)

#11 @boonebgorges
8 years ago

  • Owner changed from ryanplas to boonebgorges

Thank you @ryanplas - The orderby clauses need sanitization too, which I'll add.

#12 @boonebgorges
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 38715:

Query: Add nicename and login params to user query.

New parameters are: login, login__in, login__not_in, nicename,
nicename__in, nicename__not_in.

login__in and nicename__in are also now valid values for the
'orderby' parameter.

Props ryanplas.
Fixes #36624.

#13 @ryanplas
8 years ago

@boonebgorges oops - my bad. Thanks for taking the time to review my patch!

Note: See TracTickets for help on using tickets.