WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 9 months ago

Last modified 9 months ago

#21426 closed enhancement (fixed)

Add get and set methods to WP_User_Query

Reported by: wonderboymusic Owned by: nacin
Priority: normal Milestone: 3.5
Component: Query Version: 3.1
Severity: normal Keywords: has-patch commit
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 11 months ago.
21426.diff (2.7 KB) - added by nacin 9 months ago.
wp-user-query-tests.diff (1.3 KB) - added by wonderboymusic 9 months ago.

Download all attachments as: .zip

Change History (14)

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

comment:2 scribu11 months ago

  • Milestone changed from Awaiting Review to 3.5

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

comment:4 nacin9 months ago

False or null for when it isn't set?

comment:5 wonderboymusic9 months 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

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

nacin9 months ago

comment:7 nacin9 months ago

  • Keywords commit needs-unit-tests added

21426.diff — could use some basic unit tests.

comment:8 wonderboymusic9 months ago

I am noticing this trend

comment:9 wonderboymusic9 months ago

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

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

comment:11 nacin9 months ago

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