Discussion:
Querying users, orderby last name field?
Josh Pollock
2013-12-20 20:25:29 UTC
Permalink
There doesn't seem to be an orderby option in WP_User_Query to orderby last
name field. This leaves me wondering why not and what the most direct way
to do so would be.
Otto
2013-12-20 20:38:51 UTC
Permalink
Post by Josh Pollock
There doesn't seem to be an orderby option in WP_User_Query to orderby last
name field. This leaves me wondering why not and what the most direct way
to do so would be.
Does this not work?

new WP_User_Query(array(
'meta_key'=>'last_name',
'orderby'=>'meta_value',
));

The last name is not a field that exists in the users table. It's a meta
field. So you have to specify it and then order by the value of it.

-Otto
Josh Pollock
2013-12-21 01:21:30 UTC
Permalink
Otto-

Works like a charm. I ended up needing to add `'fields'=>'all_with_meta'`
to the array so I could use 'last_name' and 'first_name' in the return
array.

Looks like this:
$users = new WP_User_Query(array(
'meta_key'=>'last_name',
'orderby'=>'meta_value',
'fields'=>'all_with_meta'
));

Thanks,
Josh
Post by Otto
Post by Josh Pollock
There doesn't seem to be an orderby option in WP_User_Query to orderby
last
Post by Josh Pollock
name field. This leaves me wondering why not and what the most direct way
to do so would be.
Does this not work?
new WP_User_Query(array(
'meta_key'=>'last_name',
'orderby'=>'meta_value',
));
The last name is not a field that exists in the users table. It's a meta
field. So you have to specify it and then order by the value of it.
-Otto
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...