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

如何自动隐藏WordPress中ACF日期字段自定义文章类型的过期活动

解决方案

你只需要给现有WP_Query添加日期过滤规则,查询时直接排除过期活动即可,该方式比查询所有活动后再做前端判断性能更优。

修改说明

  • ACF日期字段默认存储格式为Ymd(即无分隔符的年月日格式,示例:20240520),我们先生成和该格式匹配的当日日期
  • 在WP_Query参数中新增meta_query配置,筛选出活动日期大于等于当日的自定义文章

完整修改后代码

<div class="row" id="gallerydate">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// 生成当前日期,格式和ACF日期字段存储格式保持一致
$today = date('Ymd');
$loop = new WP_Query( array( 
'post_type' => 'calendar',
'posts_per_page' => 8,
'meta_key'  => 'date_du_concert',
'orderby' => 'meta_value',
// 如果需要最近的活动排在最前面,可把order改为ASC
'order' => 'DESC',
'paged' => $paged,
// 新增日期过滤规则
'meta_query' => array(
    array(
        'key' => 'date_du_concert',
        'value' => $today,
        'compare' => '>=',
        'type' => 'DATE'
    )
)
)
);
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-3 col-6">
<div class="cardagenda">
<?php //the_post_thumbnail('large', array('class' => 'cover-img')); ?>
<img src="<?php echo get_the_post_thumbnail_url( null, 'large' ); ?>" class="cover-img wp-post-image">
<?php if (get_field('statut')): ?>
<div class="statutagenda"><?php the_field( 'statut' ); ?></div>
<?php endif; ?>
<div class="dateagenda">
<?php the_field( 'date_du_concert' ); ?>
</div>
<div class="lieuagenda">
<?php the_field( 'lieu_du_concert' ); ?>
</div>
                        <div class="ticketagenda">
                            <?php $artiste = get_field( 'artiste' ); ?>
                            <?php if ( $artiste ): ?>
                            <?php foreach ( $artiste as $post ):  ?>
                                <?php setup_postdata ( $post ); ?>
                                <a href="<?php the_permalink(); ?>">Infos & tickets</a>
                            <?php endforeach; ?>
                            <?php wp_reset_postdata(); ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endwhile; ?>
                <?php wp_reset_postdata();
                endif; ?>

特殊情况说明

如果你在ACF字段设置中修改了date_du_concert的存储格式,只需要把$today = date('Ymd');中的格式调整为和你设置的存储格式完全一致即可,比如你设置的是Y-m-d就改成$today = date('Y-m-d');。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 10:54:04