admin管理员组文章数量:1130349
For quite a while I have been trying to query posts, with 2 different meta_queries, and order them differently. I followed the WP codex. Recently I have found out that I have been mistaken this functionality. It seems that ordering 'orderby' with multiple 'meta_key's, only works if the relation between 2 meta_queries is AND. Meaning that when queried posts have the same value for the first 'orderby' meta_key, then it orders them based on the second 'orderby' meta_key. Thus a query like below works, because of the 'relation' => 'AND', and will sort posts based on 'state_clause', if posts have the same value for 'city_clause'. Correct?
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'compare' => 'EXISTS',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
) );
Question
What I have been trying to do however, is order posts differently that only have one of the two custom fields. Using the example above, I have some posts with the custom field 'state' and some posts with the custom field 'city'. What I want to do is first show the posts that have the CF 'city' and order them ASC, after that I want to show the posts that have the CF 'state' and order them DESC. I have tried a code similar to the example above, but changed the relation to 'relation' => 'OR',. It queried the posts that I needed, but as I understand now it did not sort the posts as I wanted. So I am wondering if it is possible to accomplish the above, without using 2 different WP queries?
Thanks for reading.
For quite a while I have been trying to query posts, with 2 different meta_queries, and order them differently. I followed the WP codex. Recently I have found out that I have been mistaken this functionality. It seems that ordering 'orderby' with multiple 'meta_key's, only works if the relation between 2 meta_queries is AND. Meaning that when queried posts have the same value for the first 'orderby' meta_key, then it orders them based on the second 'orderby' meta_key. Thus a query like below works, because of the 'relation' => 'AND', and will sort posts based on 'state_clause', if posts have the same value for 'city_clause'. Correct?
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'compare' => 'EXISTS',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
) );
Question
What I have been trying to do however, is order posts differently that only have one of the two custom fields. Using the example above, I have some posts with the custom field 'state' and some posts with the custom field 'city'. What I want to do is first show the posts that have the CF 'city' and order them ASC, after that I want to show the posts that have the CF 'state' and order them DESC. I have tried a code similar to the example above, but changed the relation to 'relation' => 'OR',. It queried the posts that I needed, but as I understand now it did not sort the posts as I wanted. So I am wondering if it is possible to accomplish the above, without using 2 different WP queries?
Thanks for reading.
本文标签: wp queryUnderstanding the orderby in WPQuery
版权声明:本文标题:wp query - Understanding the orderby in WP_Query? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749025574a2305050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论