简体   繁体   English

按日期对Wordpress自定义元框值进行排序

[英]Sorting Wordpress Custom Meta Box value by date

I'm working on a custom events post type for a wordpress theme and I'm running into a sorting problem. 我正在为wordpress主题创建自定义事件发布类型,但遇到了排序问题。 Everything works as I need until I spit out the values to the template. 一切都按我需要的工作,直到将值吐给模板为止。 I need the events to be listed in order by date, but I can't seem to get that to work. 我需要按日期顺序列出事件,但似乎无法正常工作。 Here's my code: 这是我的代码:

// 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));
}

Above I'm getting the input from the date picker and reformatting the text. 在上面,我从日期选择器获取输入并重新格式化文本。

// 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 );

Here I'm using WP_Query to grab the proper post type, the meta key, and I'm trying to sort the output via that meta key. 在这里,我使用WP_Query来获取正确的帖子类型,即元键,并且尝试通过该元键对输出进行排序。 The only problem is that the events aren't sorted properly. 唯一的问题是事件未正确排序。

I've tried converting the _cmb_event_date to be an integer, but I couldn't get that to work properly. 我尝试将_cmb_event_date转换为整数,但是无法正常运行。 I thought that maybe the dates weren't sorting right because they weren't an integer, but a string. 我认为日期可能排序不正确,因为它们不是整数,而是字符串。

I've looked through a bunch of posts here on Stack and I can't seem to find an answer that works for me. 我在Stack上浏览了很多帖子,但似乎找不到适合我的答案。 Any help is much appreciated! 任何帮助深表感谢! If you need more context for the code, let me know. 如果您需要更多代码上下文,请告诉我。

Issue solved. 问题已解决。 'order_by' => 'meta_value_num', should have been 'orderby' => 'meta_value_num', . 'order_by' => 'meta_value_num',应该是'orderby' => 'meta_value_num',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM