如何按自定义日期对WordPress查询进行排序


How to sort the WordPress Query by a custom date?

所以我有一个名为"Events"的自定义帖子类型。在每个事件中,我都有一个名为"start_time_date"的自定义字段,它返回一个类似"08/01/12"的日期。我该如何对这些帖子进行排序以返回帖子,从而使帖子按start_time_date升序排序(首先显示的是更接近当前日期的start_time_date)。

这是我当前的代码:

<? query_posts("cat=$currentID&showposts=100"); ?>
<? if ( have_posts() ) : ?>
    <? while ( have_posts() ) : the_post(); if (time() < strtotime(get('end_time_date'))) :?>
       <? $originalDate = get('start_time_date'); $newDate = date("M j, Y", strtotime($originalDate)); ?>
       <div class="post">Some Post Data</div>
    <? endif; endwhile; ?>
<? endif; ?>

你有没有尝试过类似的东西:

<? query_posts("cat=$currentID&showposts=100&orderby=date&order=asc"); ?>

我不确定date到底在数据库列中是什么,但它是这样的。此外,订单可能需要更改,但我认为这无关紧要。

编辑

我做了更多的研究,&orderby=date&order=asc的日期和订购是正确的。

很抱歉,你读过WordPress代码中使用自定义选择查询显示帖子吗?

他们还添加了query_posts('&meta_key=popularity&orderby=meta_value');,其中meta_key是自定义字段的名称。

在这里找到:类引用/WP查询

使用一个元字段,该字段以mysql格式存储日期,即该帖子的2012-08-02。也许可以考虑使用jquery日期选择器为您呈现这种格式。

保存后,这篇文章现在应该在wp_postmeta表中有一行。

$results = new WP_Query( array( 'meta_key' => 'date', 'orderby'=> 'meta_value' ));