WP_QUERY将数字排序为字母

时间:2020-08-27 作者:joann2555

我在使用ACF插件创建的用户配置文件中有一些meta\\u字段。我正在尝试运行查询以获取具有以下条件的用户:

用户必须具有meta\\u字段jogador_profissional == truequantidade_corretas (ASC) 以及tempo_total (DESC). 两者都是数值



    $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();

它运行正常,但我怀疑此查询将数值视为字符串值:例如,9 > 1002 > 18.

使此订单以数字方式工作,我做错了什么?

谢谢

1 个回复
最合适的回答,由SO网友:shanebp 整理而成

要将元值计算为数字,请使用meta_value_num.

尝试:

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

);