简体   繁体   中英

Removing custom posts type posts from array, based on ACF date-time value

I want to show posts by the date time field in ACF. My posts are sorted so that the closest to todays date is the first in the list and posts with a date in the past, that post is not included in the array.

I tried the different solutions i were able to find in here with no luck.

What my code looks like:

PHP Code:-

$today = date('Y-m-d H:i:s');

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'arrangement',
    'meta_query'    => array(
                    array(
                        'key'       => 'date',
                        'compare'   => '>=',
                        'value'     => $today,
                    'type'  => 'DATETIME',
                    )
            ),
    'order'             => 'ASC',
    'orderby'           => 'meta_value_num',
    'meta_key'          => 'date',
    'meta_type'         => 'DATE'
));

When I am showing the posts:

<?php while ( have_posts() ) : the_post(); ?>

EDIT: With the code above, the post that has a date in the past is shown in the while loop, but no data from the post is rendered.

如果我没记错的话,ACF在内部将datetime字段保存为时间戳,因此您必须将元查询更改为数字比较,并使用time()而不是格式化的日期时间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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