WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#21426 closed enhancement (fixed)

Add get and set methods to WP_User_Query

Reported by: wonderboymusic Owned by: nacin
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: has-patch commit
Focuses: Cc:

Description

User queries are confusing to alter, so it would be helpful to have get and set methods a la WP_Query when altering WP_User_Query::query_vars in a filter or action.

do_action_ref_array( 'pre_user_query', array( &$this ) );

Without these methods, you have to do things like:

$query->query_vars[ 'meta_query' ] = array( $memory_hog );

Instead of:

$query->set( 'meta_query', array( $memory_hog )  );

Attachments (3)

user-query-get-set.diff (2.7 KB) - added by wonderboymusic 7 years ago.
21426.diff (2.7 KB) - added by nacin 7 years ago.
wp-user-query-tests.diff (1.3 KB) - added by wonderboymusic 7 years ago.

Download all attachments as: .zip

Change History (14)

#1 @scribu
7 years ago

I personally don't find WP_Query->set() and WP_Query->get() all that useful, but I do use them, so I don't know. +1 for consistency, I guess.

#2 @scribu
7 years ago

  • Milestone changed from Awaiting Review to 3.5

#3 @nacin
7 years ago

One thing I don't like about WP_Query::get() is that it returns an empty string, rather than something like null or false. See #19243. It would be nice if we changed how WP_Query::get() works in the process.

#4 @nacin
7 years ago

False or null for when it isn't set?

#5 @wonderboymusic
7 years ago

I like functions to return absolutely nothing in those cases, but you pick false / null if there's a consistency issue with WP_Query

#6 @nacin
7 years ago

null would be the same as absolutely nothing, so that's fine. I may try to convert WP_Query::get() over to null in the future.

@nacin
7 years ago

#7 @nacin
7 years ago

  • Keywords commit needs-unit-tests added

21426.diff — could use some basic unit tests.

#8 @wonderboymusic
7 years ago

I am noticing this trend

#9 @wonderboymusic
7 years ago

Attached tests - adds query.php to tests/user in Unit Tests

#10 @nacin
7 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [21995]:

Add get() and set() methods to WP_User_Query for modifying query variables the way you might with WP_Query. props wonderboymusic. fixes #21426.

#11 @nacin
7 years ago

  • Keywords needs-unit-tests removed
Note: See TracTickets for help on using tickets.