Php 按日期排序Wordpress自定义元框值
我正在为wordpress主题编写一个自定义的事件帖子类型,我遇到了一个排序问题。在我将值输出到模板之前,一切都按我需要的方式工作。我需要将事件按日期顺序列出,但我似乎无法实现这一点。这是我的密码:Php 按日期排序Wordpress自定义元框值,php,wordpress,sorting,loops,meta,Php,Wordpress,Sorting,Loops,Meta,我正在为wordpress主题编写一个自定义的事件帖子类型,我遇到了一个排序问题。在我将值输出到模板之前,一切都按我需要的方式工作。我需要将事件按日期顺序列出,但我似乎无法实现这一点。这是我的密码: // From my custom meta box. Shows the type of field that // the user enters the date into. The prefix is _cmb_ array( 'name' => 'Event Date',
// From my custom meta box. Shows the type of field that
// the user enters the date into. The prefix is _cmb_
array(
'name' => 'Event Date',
'id' => $prefix . 'event_date',
'type' => 'text_date'
),
---- // -----
// Grab the date that's input from the user in the
// Custom Meta Box and Convert the text_date to
// YYYYMMDD format
if ( $field['type'] == 'text_date' ) {
$new = date('omd', strtotime($new));
}
上面我从日期选择器获取输入并重新格式化文本
// Tour Dates Loop
$args = array(
'post_type' => 'event',
'meta_key' => '_cmb_event_date',
'order_by' => 'meta_value_num',
'order' => 'DESC'
);
$tourDates = new WP_Query( $args );
在这里,我使用WP_查询来获取适当的post类型,即元键,并尝试通过该元键对输出进行排序。唯一的问题是事件没有正确排序
我已尝试将\u cmb\u event\u date
转换为整数,但无法使其正常工作。我想可能日期排序不对,因为它们不是整数,而是字符串
我浏览了Stack上的一堆帖子,似乎找不到适合我的答案。非常感谢您的帮助!如果您需要代码的更多上下文,请告诉我。问题已解决
'order\u by'=>'meta\u value\u num',
应该是'orderby'=>'meta\u value\u num',
进一步检查后,我用来创建自定义元框的库似乎将日期转换为字符串,即使我使用strotime()
将日期转换为时间戳进行排序。难怪分拣工作不正常!