ORDER BY定制字段值OUT WHERE子句

时间:2016-02-24 作者:dkatwp

基于此处的示例https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

我想修改查询

$q = new WP_Query( array(
\'meta_query\' => array(
    \'relation\' => \'AND\',
    \'state_clause\' => array(
        \'key\' => \'state\',
        \'value\' => \'Wisconsin\',
    ),
    \'city_clause\' => array(
        \'key\' => \'city\',
        \'compare\' => \'EXISTS\',
    ), 
),
\'orderby\' => \'city_clause\') );
能够在以下位置获取所有帖子state 是“威斯康星州”还是state 是“California”并且订单population 描述。

我在WP中写这样的查询失败了。

任何提示都将不胜感激。

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

定义必须设置的填充,并将允许的状态列为一个数组。

之后,将人口纳入orderby 并对其进行排序DESC.

$q = new WP_Query( array(
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        \'state_clause\' => array(
            \'key\' => \'state\',
            \'value\' => array( \'Wisconsin\', \'California\' ), //allowed values
            \'compare\' => \'IN\' // state must be in array above
        ),
        \'population_clause\' => array(
            \'key\' => \'population\',
            \'compare\' => \'EXISTS\',
        ), 
    ),
    \'orderby\' => array(
        \'population_clause\' => \'DESC\',
    ),
) );
将两种不同的条件与OR:

$q = new WP_Query( array(
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        \'state_clause\' => array(
            \'key\' => \'state\',
            \'value\' => array( \'Wisconsin\' ), //allowed values
            \'compare\' => \'IN\' // state must be in array above
        ),
        \'state_clause\' => array(
            \'key\' => \'timezone\',
            \'value\' => \'central,
            \'compare\' => \'=\'
        ),
        \'population_clause\' => array(
            \'key\' => \'population\',
            \'compare\' => \'EXISTS\',
        ), 
    ),
    \'orderby\' => array(
        \'population_clause\' => \'DESC\',
    ),
) );

相关推荐

WooCommerce sort by SKU

所以我试过了https://gist.github.com/bekarice/1883b7e678ec89cc8f4d 在我的网站上。只要所有产品都有SKU,效果就很好。如果产品没有SKU,则根本不会显示。也就是说,是否有其他方法可以按SKU排序,但不要求产品实际具有SKU?