wordpress调用今日展会和近期展会
方案一:
整体思路
- 今日展会:
Exhibition_start_time <= 今天结束且Exhibition_end_time >= 今天开始,即展会区间覆盖今天。 - 近期展会:
Exhibition_start_time > 今天结束,且在未来一定天数内(比如 30 天)。 - 日期字段如果存的是
Ymd格式(ACF 日期选择器默认格式),用meta_query比较最准;如果存的是时间戳或Y-m-d H:i:s,比较方式不同,下面按最常见的 ACF 日期字段Ymd来写。
代码
<?php
$today_start = date( 'Ymd' ) . '000000'; // 今天开始
$today_end = date( 'Ymd' ) . '235959'; // 今天结束
$future_end = date( 'Ymd', strtotime( '+30 days' ) ) . '235959'; // 未来30天
// ---------- 今日展会 ----------
$today_query = new WP_Query( array(
'posts_per_page' => 6,
'cat' => 6,
'ignore_sticky_posts' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Exhibition_start_time',
'value' => $today_end,
'compare' => '<=',
'type' => 'DATETIME',
),
array(
'key' => 'Exhibition_end_time',
'value' => $today_start,
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'meta_key' => 'Exhibition_start_time',
'order' => 'ASC',
) );
if ( $today_query->have_posts() ) : ?>
<div class="latest-news-section today-exhibition">
<h3>今日展会</h3>
<ul>
<?php while ( $today_query->have_posts() ) : $today_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date">
<?php echo get_post_meta( get_the_ID(), 'Exhibition_start_time', true ); ?>
~
<?php echo get_post_meta( get_the_ID(), 'Exhibition_end_time', true ); ?>
</span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php
// ---------- 近期展会 ----------
$upcoming_query = new WP_Query( array(
'posts_per_page' => 6,
'cat' => 6,
'ignore_sticky_posts' => true,
'meta_query' => array(
array(
'key' => 'Exhibition_start_time',
'value' => array( $today_end, $future_end ),
'compare' => 'BETWEEN',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'meta_key' => 'Exhibition_start_time',
'order' => 'ASC',
) );
if ( $upcoming_query->have_posts() ) : ?>
<div class="latest-news-section upcoming-exhibition">
<h3>近期展会</h3>
<ul>
<?php while ( $upcoming_query->have_posts() ) : $upcoming_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date">
<?php echo get_post_meta( get_the_ID(), 'Exhibition_start_time', true ); ?>
~
<?php echo get_post_meta( get_the_ID(), 'Exhibition_end_time', true ); ?>
</span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
关键点说明
1. 字段格式决定比较方式。 上面按 ACF 日期字段默认的 Ymd(如 20261105)来拼 DATETIME 比较。如果你在 ACF 里把返回格式设成了 Y-m-d H:i:s,把 $today_start 等变量改成对应格式即可,type 仍用 DATETIME。如果字段存的是 Unix 时间戳,type 要改成 NUMERIC。
2. 今日展会的判断逻辑是“区间覆盖今天”,不是“开始时间等于今天”。这样跨天的展会(比如 11 月 4 日到 6 日)在 11 月 5 日也会出现在今日展会里,符合实际。
3. 近期展会我用了 BETWEEN,范围是“今天结束之后到 30 天后”,避免和今日展会重复。如果你希望近期展会包含今天开始的,把下界改成 $today_start 即可。
4. orderby => 'meta_value' + meta_key 让展会按开始时间升序排,最近的排前面,比按发布时间排更符合展会列表的阅读习惯。
5. 两个查询都用了 wp_reset_postdata(),避免互相干扰。
需要你确认的两件事
- 这两个字段是 ACF 字段还是主题自带的自定义字段?如果是 ACF,确认字段的“返回值格式”是
Ymd还是Y-m-d H:i:s,这决定$today_start怎么拼。 - 分类 ID 你写的是
6,多 ID 时用'cat' => '929,928,927'。如果展会分了好几个分类,把cat换成category__in => array(929,928,927)更清楚。
方案二:
核心思路:别用发布日期筛,改用 meta_query 判断展期与「今天」是否重叠,再按 Exhibition_start_time 升序排。下面这段可以直接整段替换你原来的代码。
php<?php
/**
* 今日展会 + 近期展会
* 字段:Exhibition_start_time(开展开始日期和时间)
* Exhibition_end_time (展览结束日期和时间)
* 默认按 "2026-11-05 09:00:00" 这种 DATETIME 字符串存储
*/
// 用站点时区算边界,避免服务器 UTC 与前台差 8 小时
$tz = wp_timezone();
$now = new DateTimeImmutable( 'now', $tz );
$today_start = $now->setTime( 0, 0, 0 )->format( 'Y-m-d H:i:s' ); // 今天 00:00:00
$today_end = $now->setTime( 23, 59, 59 )->format( 'Y-m-d H:i:s' ); // 今天 23:59:59
$tomorrow_start = $now->modify( '+1 day' )->setTime( 0, 0, 0 )->format( 'Y-m-d H:i:s' );
$near_end = $now->modify( '+30 days' )->setTime( 23, 59, 59 )->format( 'Y-m-d H:i:s' );
$cat_id = 6; // 换成你的分类 ID;多 ID 写 '929,928,927'(命中任一分类)
?>
<!-- ===== 今日展会:展期覆盖今天(今天正在举办,含跨天在展) ===== -->
<?php
$today_query = new WP_Query( array(
'posts_per_page' => -1, // 今日在展通常不多,全显;改 6 可限制
'cat' => $cat_id,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key' => 'Exhibition_start_time',
'value' => $today_end, // 开始时间 <= 今天结束
'compare' => '<=',
'type' => 'DATETIME',
),
'end_clause' => array(
'key' => 'Exhibition_end_time',
'value' => $today_start, // 结束时间 >= 今天开始
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => array( 'start_clause' => 'ASC' ), // 按开展时间升序,不是发布时间
) );
if ( $today_query->have_posts() ) : ?>
<div class="latest-news-section today-exhibition">
<h3>今日展会</h3>
<ul>
<?php while ( $today_query->have_posts() ) : $today_query->the_post();
$s = get_post_meta( get_the_ID(), 'Exhibition_start_time', true );
$e = get_post_meta( get_the_ID(), 'Exhibition_end_time', true ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="exh-range"><?php echo esc_html( $s . ' ~ ' . $e ); ?></span>
<span class="post-date"><?php echo get_the_date( 'Y-m-d' ); ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p>今日暂无展会。</p>
<?php endif; ?>
<!-- ===== 近期展会:明天起 30 天内开幕、且尚未结束 ===== -->
<?php
$near_query = new WP_Query( array(
'posts_per_page' => 6,
'cat' => $cat_id,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key' => 'Exhibition_start_time',
'value' => array( $tomorrow_start, $near_end ),
'compare' => 'BETWEEN',
'type' => 'DATETIME',
),
'end_clause' => array(
'key' => 'Exhibition_end_time',
'value' => $today_start, // 排除已结束的
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => array( 'start_clause' => 'ASC' ),
) );
if ( $near_query->have_posts() ) : ?>
<div class="latest-news-section upcoming-exhibition">
<h3>近期展会</h3>
<ul>
<?php while ( $near_query->have_posts() ) : $near_query->the_post();
$s = get_post_meta( get_the_ID(), 'Exhibition_start_time', true );
$e = get_post_meta( get_the_ID(), 'Exhibition_end_time', true ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="exh-range"><?php echo esc_html( $s . ' ~ ' . $e ); ?></span>
<span class="post-date"><?php echo get_the_date( 'Y-m-d' ); ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p>近期暂无展会安排。</p>
<?php endif; ?>
口径怎么调
- 今日展会现在是「展期覆盖今天」,所以 11/03 开幕、11/07 闭幕的展会今天也会出现在列表里,这通常是展会站最合理的口径。若你只想显示今天开幕的,把第一个
meta_query的start_clause改成value => array( $today_start, $today_end )、compare => 'BETWEEN',并删掉end_clause。 - 近期展会从明天算起,避免和「今日」重复;想连今天一起算,把
$tomorrow_start换成$today_start即可。天数改'+30 days'。 - 条数:
posts_per_page,今日用了-1(全显),近期 6 条。
最容易踩的坑:字段到底存成什么样
上面代码假设 meta_value 是 2026-11-05 09:00:00。去数据库 wp_postmeta 看一眼这两个字段的实际值,按格式对齐:
| 实际存储值 | type | 边界 format() |
|---|---|---|
2026-11-05 09:00:00 | DATETIME | Y-m-d H:i:s(当前代码) |
2026-11-05(只有日期) | DATE | Y-m-d |
1730775600(Unix 时间戳) | NUMERIC | U |
20261105(ACF 日期选择器常见格式) | NUMERIC | Ymd |
格式不统一(比如 2026/11/05 09:00 或 2026-11-05T09:00)会导致 MySQL 的 CAST(... AS DATETIME) 转出来是 NULL,查不到任何结果——这是这类代码”没报错但一条不出”的头号原因。
另外两个小点:字段名要和后台完全一致(含大小写);wp_timezone() 需要 WP 5.3+,它保证「今天」按后台设置的时区算,不会出现早上 8 点前还查到昨天的情况。