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

WordPress(ACF)仅显示今日及未来日期的事件文章

解决方案:筛选今日及未来日期的「evenement」自定义文章

你的核心问题是直接对比法语格式的日期字符串导致判断逻辑错误,因为字符串对比会按字符顺序而非实际日期大小比较。下面提供两种可行方案,优先推荐数据库层面筛选的方法(性能更优)。

方案一:数据库层面筛选(推荐)

通过修改WP_Query的查询逻辑,直接在数据库中转换法语日期并筛选,避免加载不必要的文章。

<?php
// 设置时区为巴黎,确保日期处理符合法语区规则
date_default_timezone_set('Europe/Paris');
// 获取今日日期(MySQL标准格式)
$today = date('Y-m-d');

// 构建查询参数
$loop_args = array(
    'post_type'      => 'evenement',
    'posts_per_page' => 10,
    'orderby'        => 'meta_value',
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'start_event',
            'value'   => $today,
            'compare' => '>=',
            'type'    => 'DATE'
        )
    )
);

// 添加钩子,将法语日期字符串转换为MySQL可识别的日期格式
add_filter('posts_where', function($where, $query) use ($today) {
    global $wpdb;
    // 仅针对当前自定义查询生效
    if ($query->get('post_type') === 'evenement') {
        $where = str_replace(
            "{$wpdb->postmeta}.meta_value >= '{$today}'",
            "STR_TO_DATE({$wpdb->postmeta}.meta_value, '%d %M %Y') >= '{$today}'",
            $where
        );
    }
    return $where;
}, 10, 2);

$loop = new WP_Query($loop_args);
// 移除钩子,避免影响后续查询
remove_filter('posts_where', '__return_false');
?>

<?php while ($loop->have_posts()) : $loop->the_post(); ?>
    <a href="<?php echo get_permalink(); ?>">
        <div class="image">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="description">
            <div class="dates">
                <?php echo get_field('start_event'); ?> à <?php echo get_field('hour_start_event'); ?>
            </div>
            <div class="nom">
                <?php the_title(); ?>
            </div>
        </div>
    </a>
<?php endwhile; wp_reset_postdata(); ?>

方案二:循环内判断(适合数据量小的场景)

如果无法修改数据库查询逻辑,可在循环内将法语日期转换为时间戳后对比:

<?php
date_default_timezone_set('Europe/Paris');
// 获取今日0点的时间戳
$today_timestamp = strtotime('today');

// 手动映射法语月份到英文(避免依赖服务器语言环境)
function french_to_english_month($date_str) {
    $month_map = array(
        'Janvier'   => 'January',
        'Février'   => 'February',
        'Mars'      => 'March',
        'Avril'     => 'April',
        'Mai'       => 'May',
        'Juin'      => 'June',
        'Juillet'   => 'July',
        'Août'      => 'August',
        'Septembre' => 'September',
        'Octobre'   => 'October',
        'Novembre'  => 'November',
        'Décembre'  => 'December'
    );
    return str_replace(array_keys($month_map), array_values($month_map), $date_str);
}

$loop = new WP_Query(array(
    'post_type'      => 'evenement',
    'posts_per_page' => 10
));
?>

<?php while ($loop->have_posts()) : $loop->the_post(); ?>
    <?php
    $event_date_str = get_field('start_event');
    // 转换为可识别的日期格式并生成时间戳
    $event_timestamp = strtotime(french_to_english_month($event_date_str));
    
    if ($event_timestamp >= $today_timestamp) :
    ?>
        <a href="<?php echo get_permalink(); ?>">
            <div class="image">
                <?php the_post_thumbnail(); ?>
            </div>
            <div class="description">
                <div class="dates">
                    <?php echo $event_date_str; ?> à <?php echo get_field('hour_start_event'); ?>
                </div>
                <div class="nom">
                    <?php the_title(); ?>
                </div>
            </div>
        </a>
    <?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>

关键注意事项

  1. 必须设置正确的时区(如Europe/Paris),确保日期计算准确。
  2. 若使用方案二的自动转换(不手动映射月份),需确保服务器已安装法语语言包(fr_FR.UTF-8),否则strtotime无法识别法语月份。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:58:14