0

I know that normal WP_Query's can have an "orderby" parameter of "meta_value_num", but when I try to user "orderby" => "meta_value_num" on a WP_User_Query, it doesn't seem to work. Anyway I can obtain this functionality or is there something I'm missing?

Edit:

Here's what I've tried to retrieve results ordered by numeric meta values:

$sorted_users = new WP_User_Query(array(
    'orderby' => 'meta_value',
    'meta_key' => 'satisfaction_rating',
    'order' => $order,
    'role' => 'subscriber',
    'meta_query' => array(
        array(
            'key' => 'satisfaction_rating',
            'type' => 'NUMERIC'
        )
    )
));

Still doesn't seem to work...

1

This is untested, but I believe if you set type in your meta query to numeric, that will cast the values as numbers so orderby meta_value should work correctly.

  • Please see my edit above. It doesn't seem to work but I'm not sure if I'm doing anything wrong. – Thought Space Designs Feb 24 '15 at 20:57
  • You're not doing any sort of comparison, so it defaults to =, but you haven't set any value. If you just want to query on the user having that key with any value, you need to set compare to EXISTS in the meta query. – Milo Feb 24 '15 at 21:30
  • Alright. I'm kind of debating doing this all with a SQL query anyway as my code isn't very efficient at this point. I'm having a hard time finding where to start with the SQL query and I put another question up. I'll mark this answer as correct. Do you mind taking a look over at this question: wordpress.stackexchange.com/questions/179274/… – Thought Space Designs Feb 24 '15 at 21:43
0

In WP_User_Query() you probably want to set orderly to 'meta_value_num' instead of 'meta_value' as well.

This is working with WP 4.7+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.