You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义webinar文章类型区分即将/往期活动展示异常

问题根源

代码存在4个核心问题导致展示异常:

  • 日期比对格式不统一:date('Ymd')生成的是纯数字格式日期(如20240520),但ACF字段date_posted默认返回带分隔符的日期字符串,转时间戳后数值比对完全失准,所有未来日期都会被判定为小于当前日期,全部归到往期板块。
  • 循环结束后未重置全局文章对象:两次WP_Query执行完都没有调用wp_reset_postdata(),会造成全局查询数据错乱。
  • 筛选逻辑性能差:日期判断写在循环内部,相当于拉取全量数据后再用PHP过滤,远不如WordPress原生meta查询效率高,且容易出现边界判断错误。
  • 附带功能bug:活动卡片的标题链接为空,展示的日期是文章后台发布日期而非webinar实际举办日期。
修复步骤

1. 统一ACF日期字段格式

进入后台自定义字段设置页,找到date_posted字段,将返回格式设置为Ymd,和比对用的日期格式完全统一。

2. 替换模板代码

将模板中原有两个webinar板块的全部代码替换为以下优化版本,直接通过WP_Query原生参数做日期筛选,同时修复附带bug:

<!-- 即将举办的Webinar板块 -->
<section class="section-padding">
    <div class="container">
        <div class="row">
            <div class="col-md-12 text-center">
                <h2 class="pb-30">Upcoming Webinars</h2>
                <hr class="serprate-line" style="background-image: linear-gradient(to right, transparent, grey, transparent);">
            </div>
        </div>
    </div>
</section>
<section class="page-wrapper">
    <div class="container">
        <div class="row justify-content-center">
<?php
$current_date = date('Ymd');
$upcoming_args = array(
    'post_type' => 'webinar',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'date_posted',
    'meta_query' => array(
        array(
            'key' => 'date_posted',
            'value' => $current_date,
            'compare' => '>=',
            'type' => 'NUMERIC'
        )
    )
);
$upcoming_query = new WP_Query($upcoming_args);
if ($upcoming_query->have_posts()) :
    while ($upcoming_query->have_posts()) : $upcoming_query->the_post();
        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(), 'full');?>
        <div class="col-lg-4 col-md-6">
            <div class="webinar-wrapper">
                <a href="<?php echo get_the_permalink(); ?>"><img src="<?php echo $featured_img_url; ?>"></a>
                <div class="webinar-title">
                    <h5><a href="<?php echo get_the_permalink(); ?>"><?php echo get_field('webinar_heading'); ?></a></h5>
                </div>
                <span class="pDate"><?php echo date('d M, Y', strtotime(get_field('date_posted'))); ?></span>
            </div>
        </div>
<?php
    endwhile;
    wp_reset_postdata();
else:
    echo '<p class="text-center w-100">暂无即将举办的线上研讨会</p>';
endif;
?>
        </div>
    </div>
</section>

<!-- 往期Webinar板块 -->
<section class="section-padding">
    <div class="container">
        <div class="row">
            <div class="col-md-12 text-center">
                <h2 class="pb-30">Past Webinars</h2>
                <hr class="serprate-line" style="background-image: linear-gradient(to right, transparent, grey, transparent);">
            </div>
        </div>
    </div>
</section>
<section class="page-wrapper">
    <div class="container">
        <div class="row justify-content-center">
<?php
$past_args = array(
    'post_type' => 'webinar',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'order' => 'DESC',
    'orderby' => 'meta_value',
    'meta_key' => 'date_posted',
    'meta_query' => array(
        array(
            'key' => 'date_posted',
            'value' => $current_date,
            'compare' => '<',
            'type' => 'NUMERIC'
        )
    )
);
$past_query = new WP_Query($past_args);
if ($past_query->have_posts()) :
    while ($past_query->have_posts()) : $past_query->the_post();
        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(), 'full');?>
        <div class="col-lg-4 col-md-6">
            <div class="webinar-wrapper">
                <a href="<?php echo get_the_permalink(); ?>"><img src="<?php echo $featured_img_url; ?>"></a>
                <div class="webinar-title">
                    <h5><a href="<?php echo get_the_permalink(); ?>"><?php echo get_field('webinar_heading'); ?></a></h5>
                </div>
                <span class="pDate"><?php echo date('d M, Y', strtotime(get_field('date_posted'))); ?></span>
            </div>
        </div>
<?php
    endwhile;
    wp_reset_postdata();
else:
    echo '<p class="text-center w-100">暂无往期线上研讨会</p>';
endif;
?>
        </div>
    </div>
</section>

分类法使用说明

你之前注册的webinar_category自定义分类法可用于按内容主题归类webinar,发布内容时直接在编辑页勾选对应分类即可。如果需要在查询时筛选指定分类的webinar,直接在查询参数中增加tax_query配置项即可,无需改动现有时间筛选逻辑。


内容的提问来源于stack exchange,提问作者Mohit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 18:18:51