#27887 closed enhancement (fixed)
Add orderby meta_value_num option for WP_User_Query
Reported by: | genkisan | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | 3.9 |
Component: | Users | Keywords: | has-patch |
Focuses: | template | Cc: |
Description
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)
Change History (14)
#2
@
10 years ago
Tried with 'type' => 'UNSIGNED' but sorting is still incorrect (same results: 1, 3, 34, 4, 56, 6).
#3
@
10 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');
#4
@
10 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
@
10 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
@
10 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
.
@
10 years ago
Adding meta_value_num orderby option for WP_User_Query. Including a test for that case. Documenting the new orderby option.
#8
@
10 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 includemeta_value_num
.
#10
@
10 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 31369:
#11
@
10 years ago
#31265 is a general ticket for aligning 'orderby' meta support across query classes.
Doesn't setting
'type' => 'UNSIGNED'
for a meta field cause it's values to be cast toUNSIGNED
? Does that not work when using'orderby' => 'meta_value'
?E.g.:
(I haven't taking the time to test this yet, so I could be wrong.)