1

I have some meta_fields in user profile created with ACF plugin. I am trying to run a query to get users with the following criteria:

  • User must have meta_field jogador_profissional == true
  • List of users must be ordered by meta_fields quantidade_corretas (ASC) AND tempo_total (DESC). Both are numeric values.

This is my query:



    $args = array(
        "fields" => "ids",
        "meta_key" => "jogador_oficial",
        "meta_value" => true,
        "meta_query" => array(
        
            "relation" => "AND",
            "query_one" => array(
                "key" => "quantidade_corretas"
            ),
            "query_two" => array(
                "key" => "tempo_total"
            ),
        ),
        "orderby" => array(
            "query_one" => "DESC",
            "query_two" => "ASC"
        ),

    );

    $user_query = new WP_User_Query($args);

    $users = $user_query->get_results();

It runs OK, but i suspect that this query is treating numeric values as string values: for example, 9 > 100 or 2 > 18.

What am i doing wrong to make this order works in a numeric way?

Thanks.

1 Answer 1

2

To evaluate a meta value as a number, use meta_value_num.

Try:

$args = array(
    "fields" => "ids",
    "meta_key" => "jogador_oficial",
    "meta_value" => true,
    "meta_query" => array(
    
        "relation" => "AND",
        "query_one" => array(
            "key" => "quantidade_corretas"
            "orderby" => "meta_value_num",
            "order" => "DESC"
        ),
        "query_two" => array(
            "key" => "tempo_total"
            "orderby" => "meta_value_num",
            "order" => "ASC"
        ),
    ),

);
1
  • Thank you, this worked. WP documentation wasn't very clear about named queries.
    – joann2555
    Aug 28, 2020 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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