Make WordPress Core

Opened 9 years ago

Closed 8 years ago

Last modified 2 years ago

#27887 closed enhancement (fixed)

Add orderby meta_value_num option for WP_User_Query

Reported by: genkisan's profile genkisan Owned by: boonebgorges's profile boonebgorges
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.9
Component: Users Keywords: has-patch
Focuses: template Cc:


Using 'orderby' => 'meta_value', "sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use 'meta_value_num' instead for numeric values." (lifted from WP_Query).

So as with WP_Query, the meta_value_num option is needed in WP_User_Query for proper numeric sorting.

Attachments (2)

user.php.patch (436 bytes) - added by genkisan 9 years ago.
27887.patch (2.6 KB) - added by tyxla 8 years ago.
Adding meta_value_num orderby option for WP_User_Query. Including a test for that case. Documenting the new orderby option.

Download all attachments as: .zip

Change History (14)

9 years ago

#1 @jdgrimes
9 years ago

  • Keywords has-patch reporter-feedback added

Doesn't setting 'type' => 'UNSIGNED' for a meta field cause it's values to be cast to UNSIGNED? Does that not work when using 'orderby' => 'meta_value'?


$query = new WP_User_Query(
            'meta_query' => array( array( 'key' => 'my_key', 'type' => 'UNSIGNED' ) ),
            'orderby' => 'meta_value',

(I haven't taking the time to test this yet, so I could be wrong.)

#2 @genkisan
9 years ago

Tried with 'type' => 'UNSIGNED' but sorting is still incorrect (same results: 1, 3, 34, 4, 56, 6).

#3 @genkisan
9 years ago

A workaround using the pre_user_query action (modified from wp stackexchange). Would still be useful if WP_User_Query has the same meta_value_num option as WP_Query.

function my_pre_user_query($query) {
	$meta_key = $query->get('meta_key');
	$orderby = $query->get('orderby');
	if ($meta_key == 'your_key' && $orderby == 'meta_value') {
		global $wpdb;
		$query->query_orderby = ' ORDER BY ' . $wpdb->usermeta  . '.meta_value+0 ' . $query->get('order');
add_action('pre_user_query', 'my_pre_user_query');
Last edited 9 years ago by genkisan (previous) (diff)

#4 @christinecooper
9 years ago

Just to confirm, will this patch be applied to the next upgrade of Wordpress? I am referring to the addition of the meta_value_num option.

#5 @christinecooper
9 years ago

I have looked through the user.php code of WordPress 4.0 and it appears that this patch was never applied to it.

Are there any genuine plans to apply this patch to the core? Or more importantly, is there a reason why this is not applied yet? There are countless of users across the web confused about why meta_value_num is not working with WP_User_Query.

#6 @jdgrimes
9 years ago

  • Keywords reporter-feedback removed

Related: #21621

That ticket provided a more robust solution for WP_Query by always casting the ORDER BY with the meta_type. I just looked at the source and it seems that meta_type isn't currently documented for WP_User_Query, though of course it can be used because it is handled by WP_Meta_Query.

8 years ago

Adding meta_value_num orderby option for WP_User_Query. Including a test for that case. Documenting the new orderby option.

#7 @tyxla
8 years ago

#31220 was marked as a duplicate.

#8 @tyxla
8 years ago

I've just added the patch that I had submitted on the duplicate ticket #31220, which does the following:

  • Addresses the capability to sort by meta_value_num;
  • Adds a unit test for that case;
  • Updates the $orderby query parameter documentation to include meta_value_num.

#9 @boonebgorges
8 years ago

  • Milestone changed from Awaiting Review to 4.2

tyxla - Thanks for the patch. This ticket, along with #31045 and #30478, make me think that for 4.2 we should do a sweep through WP_Query, WP_Comment_Query, and WP_User_Query - the three places in core where we allow a 'meta_query' param - and make sure the 'orderby' support is identical.

#10 @boonebgorges
8 years ago

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

In 31369:

Add orderby=meta_value_num support to WP_User_Query.

Props tyxla, genkisan.
Fixes #27887.

#11 @boonebgorges
8 years ago

#31265 is a general ticket for aligning 'orderby' meta support across query classes.

This ticket was mentioned in Slack in #design by estelaris. View the logs.

2 years ago

Note: See TracTickets for help on using tickets.