CMB2 在 CPT 上按日期对数据进行排序 WP_Query
CMB2 sort data by date on CPT with WP_Query
您好,我想按活动日期对创意工坊活动进行排序吗?
我能够按其他 meta_key 作为城镇、领导者、国家/地区等对数据进行排序。但不是按事件日期。
有元 "country" 但因为它们彼此相邻,所以 UTC 没有区别。
在 CBM2 函数中,我设置了作为元框一部分的字段 "date"
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',
) ) ;
这是 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>
我不记得 CMB2 如何在数据库中存储日期字段,但您可以使用 WP_Query
的高级 meta_query
选项将自定义字段的类型指定为日期。
来自 the docs(强调我的):
meta_query also contains one or more arrays with the following keys:
- key (string) - Custom field key.
- value (string|array) - Custom field value. [...]
- compare (string) - Operator to test. [...]
- type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME',
'UNSIGNED'. Default value is 'CHAR'. You can also specify precision
and scale for the 'DECIMAL' and 'NUMERIC' types (for example,
'DECIMAL(10,5)' or 'NUMERIC(10)' are valid).
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.
如果您的日期存储为 Unix 时间戳,请与它们进行数字比较。如果它们存储为日期值,请使用 DATE
或 DATETIME
.
在尝试了一些代码之后,我找到了适合我的解决方案,但我不知道它是否是正确的代码风格。我不得不搬到时间戳土地才能让它发挥作用。
$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'
);
在foreach
循环中是
$datum = get_post_meta( $article->ID, 'workshop_data_event_date', true );
我使用 date()
通过回显将 UNIX 转换为前端的日期格式
date('d-m-Y', $datum)
编辑:因为我需要在首页显示未来 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 );
您好,我想按活动日期对创意工坊活动进行排序吗? 我能够按其他 meta_key 作为城镇、领导者、国家/地区等对数据进行排序。但不是按事件日期。
有元 "country" 但因为它们彼此相邻,所以 UTC 没有区别。
在 CBM2 函数中,我设置了作为元框一部分的字段 "date" 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',
) ) ;
这是 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>
我不记得 CMB2 如何在数据库中存储日期字段,但您可以使用 WP_Query
的高级 meta_query
选项将自定义字段的类型指定为日期。
来自 the docs(强调我的):
meta_query also contains one or more arrays with the following keys:
- key (string) - Custom field key.
- value (string|array) - Custom field value. [...]
- compare (string) - Operator to test. [...]
- type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'. You can also specify precision and scale for the 'DECIMAL' and 'NUMERIC' types (for example, 'DECIMAL(10,5)' or 'NUMERIC(10)' are valid).
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.
如果您的日期存储为 Unix 时间戳,请与它们进行数字比较。如果它们存储为日期值,请使用 DATE
或 DATETIME
.
在尝试了一些代码之后,我找到了适合我的解决方案,但我不知道它是否是正确的代码风格。我不得不搬到时间戳土地才能让它发挥作用。
$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'
);
在foreach
循环中是
$datum = get_post_meta( $article->ID, 'workshop_data_event_date', true );
我使用 date()
通过回显将 UNIX 转换为前端的日期格式
date('d-m-Y', $datum)
编辑:因为我需要在首页显示未来 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 );