‘WP_QUERY’中‘orderby’的多个自定义字段

时间:2016-12-20 作者:bhood

我试图先按“城市”排序博客帖子,然后在每个城市内按“街道名称”排序。我似乎无法按字母顺序显示“street\\u name”。我正在使用WP\\U查询:

                <?php
                $args = array(
                    \'post_type\'=> \'property\',
                    \'meta_query\' => array(
                                array(
                                    \'relation\' => \'AND\' ,
                                    array(
                                        \'meta_key\' => \'city\',
                                        \'orderby\' => \'meta_value\',
                                        \'order\' => \'ASC\'
                                    ),
                                    array(
                                        \'meta_key\' => \'street_name\',
                                        \'orderby\' => \'meta_value\',
                                        \'order\' => \'ASC\'
                                    ),
                                )
),有什么想法吗?

4 个回复
SO网友:ngearing

meta_queryorderby 是独立的参数,您只需将它们放在一个数组中即可。你必须先做一个,然后再做另一个。

e、 g。

<?php
$args = array(
    \'post_type\'     => \'property\',
    \'meta_query\'    => array(
        array(
            \'relation\' => \'AND\',
            \'city_clause\' => array(
                \'key\'       => \'city\',
                \'compare\'   => \'EXISTS\',
            ),
            \'street_clause\' => array(
                \'key\'       => \'street_name\',
                \'compare\'   => \'EXISTS\',
            ),
        )
    )
    \'orderby\' => array(
        \'city_clause\'       => \'desc\',
        \'street_clause\'     => \'desc\',
    )
)
?>

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

SO网友:Keith Gardner

我花了一些时间才弄明白如何使用多个元键按数值排序,因为您不能只使用orderby meta\\u value\\u num。相反,您可以在元查询中将类型设置为numeric。这是有效的生产代码。

    $meta_query = array(
        \'relation\' => \'AND\',
        \'query_one\' => array(
            \'key\' => \'_rama_ads_type\'
        ),
        \'query_two\' => array(
            \'key\' => \'_rama_ads_order\',
            \'type\' => \'NUMERIC\',
        ),
    );

    $order_by = array(
        \'query_one\' => \'ASC\',
        \'query_two\' => \'DESC\',
    );

    $query->set( \'meta_query\', $meta_query );
    $query->set( \'orderby\', $order_by );

SO网友:Michael H

对于meta\\u查询,您需要按特定子句排序。

此链接应涵盖所有内容,以帮助您整理语法并使其正常工作。

https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

SO网友:aayushdrolia

试试这个,可能会有帮助。

<?php
$args = array(
\'post_type\'  => \'Sports\',
\'meta_query\' => array(
    array(
        \'key\'     => \'league_count\',
                    \'orderby\' => \'meta_value_num\', /* use this only 
                             if the key stores number otherwise use \'meta_value\' */
                    \'order\' => \'ASC\'
    ),
    array(
        \'key\'     => \'matches_count\',
                    \'orderby\' => \'meta_value_num\',
                    \'order\' => \'ASC\'
    ),
),
);
$query = new WP_Query( $args );
?>