Php 如何在WordPress中仅在24小时内显示过期后的日期时间

Php 如何在WordPress中仅在24小时内显示过期后的日期时间,php,wordpress,Php,Wordpress,这是我的问题 $expiring_ads = new WP_Query( array( 'post__in' => get_option('sticky_posts'), 'post_type' => 'post', 'posts_per_page' => 5, 'no_found_rows' => true, 'orde

这是我的问题

$expiring_ads = new WP_Query( array( 
            'post__in'       => get_option('sticky_posts'), 
            'post_type'      => 'post', 
            'posts_per_page' => 5, 
            'no_found_rows'  => true,
            'orderby'        => 'meta_value',
            'order'          => 'ASC',
            'meta_query'     => array(
                                    array(
                                        'key' => 'expire_date',
                                        'value' => date("Y-m-d H:i:s"),
                                        'compare' => '>=',
                                        'type' => 'DATETIME'
                                    )
            )
        ));
我的代码显示在所有日期过期的所有帖子。如何仅在24小时内显示过期后的日期?

只需添加一个条件,通过在数据库中检查过期日期,如下所示:

// Create a filtering function to add a WHERE condition to your query like this.
function filter_expire() {
  return " AND expire_date <= '" . date('Y-m-d H:i:s', strtotime('1 day')) . "'";
}

// Add the filter to the query.
add_filter('posts_where', 'filter_expire');

// Run the query.
$expiring_ads = new WP_Query( array( 
            'post__in'       => get_option('sticky_posts'), 
            'post_type'      => 'post', 
            'posts_per_page' => 5, 
            'no_found_rows'  => true,
            'orderby'        => 'meta_value',
            'order'          => 'ASC',
            'meta_query'     => array(
                                    array(
                                        'key' => 'expire_date',
                                        'value' => date("Y-m-d H:i:s"),
                                        'compare' => '>=',
                                        'type' => 'DATETIME'
                                    )
            )
        ));
//创建一个筛选函数,将WHERE条件添加到查询中,如下所示。
函数筛选器_expire(){

return“AND expire_date@JakeGoud:I使用postETA是expire_date,并添加_filter('posts_where','filter_expire')不work@haitruonginfotech好的,我添加了另一个尝试,将
关系
添加到
元查询
。哦,是的,@JakeGoud:非常感谢much@haitruonginfotech很高兴能帮上忙!更适合WordPress开发者的网站。
// Run the query.
$expire_window = date('Y-m-d H:i:s', strtotime('1 day'));
$expiring_ads = new WP_Query( array( 
            'post__in'       => get_option('sticky_posts'), 
            'post_type'      => 'post', 
            'posts_per_page' => 5, 
            'no_found_rows'  => true,
            'orderby'        => 'meta_value',
            'order'          => 'ASC',
            'meta_query'     => array(
                                    'relation' => 'AND',
                                    array(
                                        'key' => 'expire_date',
                                        'value' => $expire_window,
                                        'compare' => '<='
                                    ),
                                    array(
                                        'key' => 'expire_date',
                                        'value' => date("Y-m-d H:i:s"),
                                        'compare' => '>=',
                                        'type' => 'DATETIME'
                                    )
            )
        ));