最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query orderby

programmeradmin2浏览0评论

I am trying to workout the best way to do a slightly abnormal orderby.

I have a query that returns 2 types (Post and Event). As expected by default it orders both, together, by publish date.

What I want is, posts to be ordered by publish date, events by event->start_date

I could just get them, loop over them and manually sort myself, but that seems counter intuitive.

Any help much appreceiated.

Cheers

Drogo

I am trying to workout the best way to do a slightly abnormal orderby.

I have a query that returns 2 types (Post and Event). As expected by default it orders both, together, by publish date.

What I want is, posts to be ordered by publish date, events by event->start_date

I could just get them, loop over them and manually sort myself, but that seems counter intuitive.

Any help much appreceiated.

Cheers

Drogo

Share Improve this question asked Mar 4, 2022 at 10:09 DrogoNevetsDrogoNevets 1012 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can do this by adding these two lines in your query:

'orderby' => 'publish_date',
'order' => 'ASC',

In the code below I assume that event->start_date is a post meta field on the Event post type.

'orderby' => array(
    'meta_value'   => 'ASC',
    'publish_date' => 'DESC'
),
'meta_key'  => 'start_date',
'meta_type' => 'DATE',

This assumes that the Posts don't have the start_date meta set, ie, that it's only present for the Events. Also I've assumed you want the Events in ascending order by their start_date and the Posts in descending publish_date order.

Reference

  • WP_Query Order/Order By

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论