简体   繁体   English

CMB2使用WP_Query在CPT上按日期对数据进行排序

[英]CMB2 sort data by date on CPT with WP_Query

Hi Im trying to sort Workshop events by date of event? 您好,我想按活动日期对Workshop活动进行排序吗? I'm able to sort data by other meta_key as town, leader, country ect. 我可以按其他meta_key(城镇,领导者,国家等)对数据进行排序。 but not by date of event. 但不是在活动日期之前。

There is meta "country" but because they are next to each other there is no difference in UTC. 这里有元“国家”,但因为它们彼此相邻,所以UTC没有区别。

In CBM2 function I have set field "date" that is part of meta box function.php 在CBM2函数中,我设置了“日期”字段,该字段是meta box function.php的一部分

<!-- language: lang-php -->
        $cmb_data->add_field( array(
            'name'        => __( 'Event Date', 'workshops' ),
            'desc'        => __( 'Event Date', 'workshops' ),
            'id'          => $prefix . 'event_date',
            'type'        => 'text_date',
            'date_format' => 'd-m-Y',
    ) ) ;

And here is CPT template workshops.php 这是CPT模板workshops.php

<!-- language: lang-php -->
      <section id="" class="">
                    <div class="">
                        <?php
                        $args = array(
                            'post_type'      => 'workshops',
                            'posts_per_page' => 10,
                            'meta_key'       => 'workshop_data_event_date',
                            'orderby'        => 'meta_value',
                            'order'          => 'DESC'
                        );

                        $articles = new WP_Query( $args );

                        if ( $articles->have_posts() ) : $articles->the_post();
                            echo '<div class="cptul-stripe-2 article-post__stats">';
                            foreach ( $articles->posts as $article ) {
    //                            var_dump($article);
                                echo '<div class="cptli-stripe-2__wrap">
                                <div class="cptli-stripe-2--header">
                                <div><span class="name">'
                                     . get_post_meta( $article->ID, 'workshop_data_leader', true )
                                     . '</span>
                                <span class="nameWhat"> povede workshop</span>
                                </div>
                                        <a class="stripe-title-mid" href="' . get_permalink( $article->ID ) . '">'
                                     . $article->post_title . '</a> 
                                         <ul>
                                            <li id="event-date" class="article-post__event-date"><span 
                                            class="article-post__stats-icon">'
                                     . webovkar_get_svg( array( 'icon' => 'calendar' ) ) . '</span>'
                                     . get_post_meta( $article->ID, 'workshop_data_event_date', true ) .

                                     '</li><li class="article-post__event-town"><span class="article-post__stats-icon"> '
                                     . webovkar_get_svg( array( 'icon' => 'mmarker' ) ) . '</span>'
                                     . get_post_meta( $article->ID, 'workshop_data_town', true ) . ' | '
                                     . get_post_meta( $article->ID, 'workshop_data_country', true ) .
                                     '</li></ul> </div>


                                     <div class="cptli-stripe-2--data">'
                                     . apply_filters( 'the_content', get_post_meta( $article->ID, 'workshop_data_excerpt',
                                        true ) ) .
                                     '</div>
                                    </div>';
                            }
                            echo '</div>';
                        endif; ?>
                        <?php wp_reset_postdata(); ?>
                    </div>
                </section> 

I don't remember offhand how CMB2 stores date fields in the database, but you can use WP_Query 's advanced meta_query options to specify the type of the custom field as a date. 我不记得CMB2如何在数据库中存储日期字段,但是您可以使用WP_Query的高级meta_query选项将自定义字段的类型指定为日期。

From the docs (emphasis mine): 文档 (重点是我的):

meta_query also contains one or more arrays with the following keys: meta_query还包含一个或多个具有以下键的数组:

  • key (string) - Custom field key. 键(字符串)-自定义字段键。
  • value (string|array) - Custom field value. 值(字符串|数组)-自定义字段值。 [...] [...]
  • compare (string) - Operator to test. 比较(字符串)-要测试的运算符。 [...] [...]
  • type (string) - Custom field type. type(字符串)-自定义字段类型。 Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME' , 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. 可能的值为“ NUMERIC”,“ BINARY”,“ CHAR”, “ DATE”,“ DATETIME” ,“ DECIMAL”,“ SIGNED”,“ TIME”,“ UNSIGNED”。 Default value is 'CHAR'. 默认值为“ CHAR”。 You can also specify precision and scale for the 'DECIMAL' and 'NUMERIC' types (for example, 'DECIMAL(10,5)' or 'NUMERIC(10)' are valid). 您还可以为“ DECIMAL”和“ NUMERIC”类型指定精度和小数位数(例如,“ DECIMAL(10,5)”或“ NUMERIC(10)”有效)。

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format. 仅当日期以YYYY-MM-DD格式存储并使用此格式进行测试时,“类型” DATE才与“比较”值BETWEEN一起使用。

If your dates are stored as Unix timestamps, compare with them numerically. 如果您的日期存储为Unix时间戳,请对其进行数字比较。 If they're stored as date values, use DATE or DATETIME . 如果将它们存储为日期值,请使用DATEDATETIME

After playing with code a bit I have find solution that is working for me but I don't know if it's right code style. 在玩了一点代码之后,我找到了适合我的解决方案,但我不知道这是否是正确的代码样式。 I had to move to timestamp land to make it work. 我必须移至时间戳记土地才能使其正常工作。

$args = array(
    'post_type' => 'workshops',
    'meta_query' => array(
        array(
            'key' => 'workshop_data_event_date',
            'value' => current_time('timestamp'),
            'compare' => '>'
        )
    ),
    'meta_type' => 'text_date_timestamp',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC'
);

In foreach loop is foreach循环中是

$datum = get_post_meta( $article->ID, 'workshop_data_event_date', true );

and Im using date() to convert UNIX to date format for front-end by echoing 和Im使用date()通过回显将UNIX转换为前端的日期格式

date('dm-Y', $datum)

EDIT: Because I need to show on front page events that will happened in upcoming 30 days I have add this Query to front page. 编辑:因为我需要在首页上显示将在未来30天内发生的事件,所以我已将此查询添加到首页。

$today = current_time('timestamp');
$featuremonth = current_time('timestamp') + 2629743 ; // num = UNIX 30 days
$meta_query = array(
    'value' => array( $today, $featuremonth),
    'compare'   => 'BETWEEN',
    'type'      => 'NUMERIC'
);
    $args = array(
        'post_type'      => 'workshops',
        'posts_per_page' => -1,
        'meta_key'       => 'workshop_data_event_date',
        'orderby'        => 'meta_value',
        'order'          => 'DESC',
        'meta_query'     => array( $meta_query )
    );

$articles = new WP_Query( $args );

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

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