admin管理员组文章数量:1130349
I have a custom post type ”events” with meta fields ”startdate” and ”enddate”. Archive query already uses meta query to show only upcoming or ongoing events, but in addition I need to order by both meta fields. I can't figure out what the meta query should be, so it works for both uses. Or am I doing something wrong?
This is part of the query, with the simple orderby that I need to replace. Anyone got any ideas how to add ”...orderby startdate, enddate” to this?
$meta_query = array(
'relation' => 'OR',
'enddate' => array (
'key' => 'enddate',
'value' => date( 'Y-m-d' ),
'compare' => '>='
),
'startdate' => array (
'key' => 'startdate',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
)
);
$query->set('meta_query',$meta_query);
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'startdate');
I have a custom post type ”events” with meta fields ”startdate” and ”enddate”. Archive query already uses meta query to show only upcoming or ongoing events, but in addition I need to order by both meta fields. I can't figure out what the meta query should be, so it works for both uses. Or am I doing something wrong?
This is part of the query, with the simple orderby that I need to replace. Anyone got any ideas how to add ”...orderby startdate, enddate” to this?
$meta_query = array(
'relation' => 'OR',
'enddate' => array (
'key' => 'enddate',
'value' => date( 'Y-m-d' ),
'compare' => '>='
),
'startdate' => array (
'key' => 'startdate',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
)
);
$query->set('meta_query',$meta_query);
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'startdate');
Share
Improve this question
asked Nov 23, 2018 at 15:54
Petri V.Petri V.
111 bronze badge
2
|
1 Answer
Reset to default 0You've already named the clauses in your meta query, so you can reference these directly in the orderby argument:
$query->set(
'orderby',
array(
'enddate' => 'DESC',
'startdate' => 'DESC'
)
);
I have a custom post type ”events” with meta fields ”startdate” and ”enddate”. Archive query already uses meta query to show only upcoming or ongoing events, but in addition I need to order by both meta fields. I can't figure out what the meta query should be, so it works for both uses. Or am I doing something wrong?
This is part of the query, with the simple orderby that I need to replace. Anyone got any ideas how to add ”...orderby startdate, enddate” to this?
$meta_query = array(
'relation' => 'OR',
'enddate' => array (
'key' => 'enddate',
'value' => date( 'Y-m-d' ),
'compare' => '>='
),
'startdate' => array (
'key' => 'startdate',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
)
);
$query->set('meta_query',$meta_query);
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'startdate');
I have a custom post type ”events” with meta fields ”startdate” and ”enddate”. Archive query already uses meta query to show only upcoming or ongoing events, but in addition I need to order by both meta fields. I can't figure out what the meta query should be, so it works for both uses. Or am I doing something wrong?
This is part of the query, with the simple orderby that I need to replace. Anyone got any ideas how to add ”...orderby startdate, enddate” to this?
$meta_query = array(
'relation' => 'OR',
'enddate' => array (
'key' => 'enddate',
'value' => date( 'Y-m-d' ),
'compare' => '>='
),
'startdate' => array (
'key' => 'startdate',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
)
);
$query->set('meta_query',$meta_query);
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'startdate');
Share
Improve this question
asked Nov 23, 2018 at 15:54
Petri V.Petri V.
111 bronze badge
2
- Welcome to WPSE! There may need to be some more context to your question. One thing that I would question is the format of the data. You'll have a hard time with querying (if >=) and sorting as a date because meta data is a string - it's never formatted as a date. One way around that is to store post meta that are dates using Unix epoch time (because that can be sorted as a string). Just a suggestion to look into. – butlerblog Commented Nov 24, 2018 at 0:45
-
Thanks for your feedback. The format is
yyyy-mm-ddwhich might not be the best way to store dates, but it worked fine before migrating the data into Wordpress. – Petri V. Commented Nov 24, 2018 at 9:34
1 Answer
Reset to default 0You've already named the clauses in your meta query, so you can reference these directly in the orderby argument:
$query->set(
'orderby',
array(
'enddate' => 'DESC',
'startdate' => 'DESC'
)
);
本文标签: How to orderby multiple meta fields with another meta query
版权声明:本文标题:How to orderby multiple meta fields with another meta query 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749155958a2324594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


yyyy-mm-ddwhich might not be the best way to store dates, but it worked fine before migrating the data into Wordpress. – Petri V. Commented Nov 24, 2018 at 9:34