简体   繁体   English

Wordpress自定义帖子类型列排序查询

[英]Wordpress custom post type column sort query

I have a custom post type called reservation . 我有一个名为reservation的自定义帖子类型。 In it I get the reservation for event/room you name it (not important). 在其中,我得到您为其命名的活动/房间的预订(不重要)。

When I go to my custom post type page, I get all the posts nicely displayed. 当我转到我的自定义帖子类型页面时,我会很好地显示所有帖子。 The first column is the reservation date. 第一列是预订日期。 Now the reservation date can be one - 28.11.2015 , but it can also be multiple dates - 28.11.2015, 29.11.2015, 02.12.2015. 现在预订日期可以是1 - 28.11.2015 ,但它也可以是多个日期 - 28.11.2015, 29.11.2015, 02.12.2015.年11月28.11.2015, 29.11.2015, 02.12.2015.

I would like to enable the sorting of that column. 我想启用该列的排序。 The problem is, this is a string, not a number so I cannot just create a simple sortable column query like described here . 问题是,这是一个字符串,而不是数字,所以我不能只创建一个简单的排序的列的查询等中描述的在这里

So far I tried this: 到目前为止我试过这个:

add_filter( 'manage_edit-reservation_sortable_columns', 'reservation_sortable_column' );

if (!function_exists('reservation_sortable_column')) {
    function reservation_sortable_column( $columns ) {
        $columns['reservation_date'] = 'reservation_date';
        return $columns;
    }
}

add_filter( 'posts_clauses', 'manage_wp_posts_be_qe_pre_get_posts', 1, 2 );

if (!function_exists('manage_wp_posts_be_qe_pre_get_posts')) {
    function manage_wp_posts_be_qe_pre_get_posts( $pieces, $query ) {
        global $wpdb;
        if ($query->is_main_query() && ( $orderby = $query->get('orderby') ) ) {
            $order = strtoupper( $query->get('order') );
            if ( !in_array( $order, array('ASC', 'DESC') ) ) {
                $order = 'ASC';
                switch ($orderby) {
                    case 'reservation_date':
                        $pieces[ 'join' ] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'reservation_date'";
                        $pieces[ 'orderby' ] = "STR_TO_DATE( wp_rd.meta_value,'%d.%m.%Y' ) $order, " . $pieces[ 'orderby' ];
                        break;
                }
            }
            return $pieces;
        }
    }
}

This doesn't work, because I'm working with strings. 这不起作用,因为我正在使用字符串。

Luckily, my dates (when a person picks them), are already sorted lowest to highest date. 幸运的是,我的日期(当一个人选择它们时)已经从最低到最高日期排序。 They are stored in $custom['reservation_date'] array, where $custom = get_post_custom($post->ID) . 它们存储在$custom['reservation_date']数组中,其中$custom = get_post_custom($post->ID) So I can get them like $custom['reservation_date'][0] . 所以我可以像$custom['reservation_date'][0]那样得到它们。

I can also separate the first date 我也可以分开第一次约会

preg_match('/^(.+?),/', $custom['reservation_date'][0], $matches);

If it's only one date, without the comma, I can then get them out like: 如果它只有一个日期,没有逗号,我可以将它们取出:

$first_date = (!empty($matches)) ? $matches[0] : $custom['reservation_date'][0];

Now what's bothering me is how to use this in my custom query? 现在困扰我的是如何在我的自定义查询中使用它? Currently the sorting is done on meta_value reservation_date (string). 目前,排序是在meta_value reservation_date (string)上完成的。 How do I make it so that I can sort based on these first dates? 我如何制作它以便我可以根据这些第一次约会进行排序?

Say I have dates like: 说我有以下日期:

19.11.2015, 20.11.2015 2015年11月19日,2015年11月20日

28.11.2015 2015年11月28日

14.11.2015, 15.11.2015 2015年11月14日,2015年11月15日

13.11.2015 2015年11月13日

When I click to sort them in ascending order I'd like to get 当我点击按升序对它们进行排序时,我想得到它

13.11.2015 2015年11月13日

14.11.2015, 15.11.2015 2015年11月14日,2015年11月15日

19.11.2015, 20.11.2015 2015年11月19日,2015年11月20日

28.11.2015 2015年11月28日

And another click on the column will order them descending 再次单击该列将命令它们降序

28.11.2015 2015年11月28日

19.11.2015, 20.11.2015 2015年11月19日,2015年11月20日

14.11.2015, 15.11.2015 2015年11月14日,2015年11月15日

13.11.2015 2015年11月13日

Any help is appreciated. 任何帮助表示赞赏。

You will not be able to sort in the WP Query as you have not stored the values in a way WordPress understands. 您将无法在WP查询中进行排序,因为您没有以WordPress理解的方式存储值。

https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

You should store dates as YYYY-mm-dd . 您应该将日期存储为YYYY-mm-dd

Either change how the dates are stored or use PHP after the database request to sort. 要么更改日期的存储方式,要么在数据库请求排序后使用PHP。

// 1. if you change how "reservation_date" is stored in the db

$posts = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'reservation',
    'meta_key' => 'reservation_date',
    'orderby' => 'meta_value',
    'order' => 'ASC'
));

// 2. if you choose to sort with PHP after pulling the data

$posts = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'reservation'
));

usort($posts, function ($post_a, $post_b) {
    $post_a_custom = get_post_custom($post_a->ID);
    $post_b_custom = get_post_custom($post_b->ID);

    $post_a_date = DateTime::createFromFormat('d.m.Y', $post_a_custom['reservation_date'][0]);
    $post_b_date = DateTime::createFromFormat('d.m.Y', $post_b_custom['reservation_date'][0]);

    if ($post_a_date == $post_b_date) {
        return 0;
    } else if ($post_a_date > $post_b_date) {
        return -1;
    } else {
        return 1;
    }
});

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

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