admin管理员组文章数量:1323714
I have a query that looks like this:
$orders = get_posts( array(
'numberofposts' => -1,
'posts_per_page' => -1,
'post_type' => 'executive-orders',
'meta_key' => 'eo_type',
'meta_value' => 'county',
'orderby' => 'date',
'order' => 'DESC',
));
And I would like to order by another meta key value. I am aware you can do this:
'orderby' => 'meta_value_num',
But is it possible to do both?
I have a query that looks like this:
$orders = get_posts( array(
'numberofposts' => -1,
'posts_per_page' => -1,
'post_type' => 'executive-orders',
'meta_key' => 'eo_type',
'meta_value' => 'county',
'orderby' => 'date',
'order' => 'DESC',
));
And I would like to order by another meta key value. I am aware you can do this:
'orderby' => 'meta_value_num',
But is it possible to do both?
Share Improve this question edited Sep 11, 2020 at 2:09 Cyclonecode 1,1841 gold badge9 silver badges32 bronze badges asked Sep 10, 2020 at 19:50 NotaGuruAtAllNotaGuruAtAll 1011 bronze badge1 Answer
Reset to default 1You can do this with a meta_query
. The get_posts()
function accepts everything that WP_Query accepts so it could look something like this:
$orders = get_posts( array(
'numberofposts' => -1,
'posts_per_page' => -1,
'post_type' => 'executive-orders',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
'eo_type' => array(
'key' => 'eo_type',
'value' => 'county',
),
'second_type' => array(
'key' => 'second_key',
'type' => 'NUMERIC',
),
),
'orderby' => array( 'eo_type' => 'DESC', 'date' => 'ASC' ),
) );
With the Meta Query we can define our key value pairs just like we did with meta_key
and meta_value
. We give them a named index eo_type
so that we can reference it in the orderby
parameter. We can add as many meta values as we need and can even nest further.
本文标签: Query by meta key and order by another meta key value
版权声明:本文标题:Query by meta key and order by another meta key value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126735a2421975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论