0

我在 Wordpress 中使用以下代码来查询事件(自定义帖子类型)并按日期按时间顺序排列它们。

query_posts( array( 'post_type' => 'events', 'showposts' => 10, 'orderby' => 'meta_value_num','meta_key' => '_ecmb_datetime' ) );

元键在哪里_ecmb_datetime,这是事件的时间戳。

我不想显示已经发生的事件,所以我的问题是如何只显示当前时间之后发生的事件,以及如何按时间顺序倒序排列?

任何帮助是极大的赞赏

4

1 回答 1

1

I'm not a WP expert, but in checking out a few resources in the Codex, it seems like you have some options. This page seems to suggest not directly altering query_posts, but if you know what you're about, so be it. This page and this one together suggest ways of modifying the query to order by arbitrary fields through a wrapper of the main function.

I might try something like:

function get_pending_events( $query ) {
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'DESC' );
    $query->set( 'meta_value', time() );
    $query->set( 'meta_compare', '>=' );
}

add_action( 'pre_get_posts', 'get_pending_events' );

Your date format needs might be different, but this might be one way to go about it. The Codex does seem to have a lot of good info.

于 2013-10-19T21:51:03.760 回答