繁体   English   中英

获取与当前日期ACF相比较的最接近日期

[英]Get closest date compared to current date ACF

我有一个很大的问题。 我将在Wordpress(WP)中将事件打印为自定义帖子+ acf。 一切正常,但我需要对事件进行排序以在顶部显示最近的事件。 有没有简单的方法可以做到这一点?

我的代码当前如下所示:

<?php

/*
Template Name: Program Template
*/
$today = date('Ymd');
$query = new WP_Query(array(
    'post_type' => 'program',
    'post_status' => 'publish'
));
get_header(); ?>
<div id="particle-canvas">
    <h1 class="page-title">Program</h1>
</div>
<div class="container-fluid program">
    <div class="row">
        <?php while ($query->have_posts()) {
            $query->the_post();
            $post_id = get_the_ID(); ?>
            <div class="col-md-6 program__program-content">
                <div class="overlay-box">
                    <span><?php $date_value = get_field('date', $post_id);
                        echo date('d/m/Y', strtotime($date_value)); ?></span>
                    <img src="<?php the_post_thumbnail_url(array(645, 246)); ?>" alt="">
                    <div class="details">
                        <h3><?php echo get_the_title(); ?></h3>
                        <div class="button">
                            <a href="<?php echo get_permalink(); ?>">
                                <p>More</p>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php wp_reset_query(); ?>
    </div>
</div>

请在查询中添加“ meta_key”,如下所示:

$query = new WP_Query(array(
    'post_type' => 'program',
    'post_status' => 'publish',
    'posts_per_page'    => -1,
    'meta_key'      => 'date',
    'meta_type'   => 'DATE',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC'
));

根据您的要求更改'order'=>'ASC / DESC'。

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM