按 ACF 日期选择器对WP_Query进行排序


Sort WP_Query by ACF Datepicker

我有一个"即将发生的事件"页面和一个"过去的事件"页面。每个事件都有一个名为"event_date"的自定义字段。

我想创建一个循环来显示比今天更大的所有事件。我已经浏览了这些文章,但无法使其正常工作:http://support.advancedcustomfields.com/forums/topic/how-do-i-filter-and-sort-event-posts-with-start-and-end-date/

https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker

WordPress 高级自定义字段按日期选择器对帖子进行排序

根据我在上面的三个链接中收集的内容,我会把它放在我的函数.php文件中:

    // CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
    if ( get_post_type( $post_id ) == 'event_type' ) {
    $event_date = get_post_meta($post_id, 'event_date', true);
        if($event_date) {
            $dateparts = explode('/', $event_date);
            $newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
            update_post_meta($post_id, 'unixstartdate', $newdate1  );
        }
    }
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

然后我会在我的页面模板中添加这样的东西:

<?php 
$today = time();
$args = array(
        'post_type' => 'event_type',
        'posts_per_page' => 5,
        'meta_query' => array(
            array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => $today,
            )
        ),
        'meta_key' => 'event_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    );
$query = new WP_Query( $args );
$event_type = $query->posts;
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

现在,这没有任何结果。我的帖子类型称为"event_type",键是"event_date"。

对我哪里出错有什么想法吗?

多亏了svsdnb,我在这里找到了解决方案。

https://wordpress.org/support/topic/query-date-array-to-display-future-events-only

不必在函数.php中转换时间戳,而是有一种方法可以专门使用 ACF 执行此操作,其中您使用

current_time('Ymd')

而不是

 $today = date ('Ymd')

这是我最终得到的(它似乎正在工作,包括今天发生的事件):

<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'event_type',
    'post_status' => 'publish',
    'posts_per_page' => '0',
    'meta_query' => array(
        array(
            'key' => 'event_date',
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    );
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>

看起来你的代码有一些问题。您的日期解析似乎也是不正确的,尽管如果没有更多信息,很难说。在函数中使用strtotime来设置unixstarttime和查询代码会很好。您的代码使用time(),其中包括秒数并省略"今天"的事件。您没有时区,因此所有内容都将被视为GMT,只要您牢记这一点就可以了。

没什么大不了的,但是您在add_action中为回调指定了两个参数,但该函数只接受一个参数。

下一个问题是have_posts()循环 - 您需要指定自定义查询$query来循环它。

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

我还以略有不同的方式实现它,使用 updated_{type}_meta 操作仅在正确的元键上触发,然后检查帖子类型。它应该只在元值更新时运行,而不是每次保存帖子时运行。我还建议按unixstarttime元值排序,因为它是数字。

函数.php

// create/update unixstartdate based on event_type.event_date update
add_action( 'updated_post_meta', 'my_updated_post_meta', 20, 4 );
function my_updated_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ){
    if ( $meta_key == 'event_date' && 'event_type' == get_post_type( $object_id ) ){
        $unixstartdate = strtotime( $_meta_value );
        update_post_meta( $object_id, 'unixstartdate', $unixstartdate );
    }
}

模板页面

$args = array(
    'post_type' => 'event_type',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => strtotime('m/d/Y', time()),
        )
    ),
    'meta_key' => 'unixstartdate',
    'orderby' => 'meta_value',
    'order' => 'ASC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
    the_title(); // whatever you want to use from $post
endwhile; endif;
<div class="row">
<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'your-custom-post-type',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => 'event_date',//your date picker field name
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'Desc',
    );
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<?php  $dateformatstring = "F d, Y";
$unixtimestamp = strtotime(get_field('event_date')); ?>
<div class="post">
    <a href="<?php the_permalink; ?>"><?php the_title(); ?></a>
 <?php echo date_i18n($dateformatstring, $unixtimestamp); ?>
</div>
 <?php
  endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>
</div>