Pergunta

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.

Foi útil?

Solução

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"
        ),
    ),

);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top