检索两个日期之间的帖子

时间:2017-11-03 10:29:23

标签: php wordpress advanced-custom-fields

我的帖子包含以下ACF自定义字段:此格式的开头日期和结束日期:Y-m-d H:i:s

我想使用WP_Query检索所有发布日期少于24小时的帖子和不到24小时的结束日期。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

试试这个代码段,

$args = array(
 'post_type' => 'surf_reports',
  'posts_per_page' => '1',
  'date_query' => array(
   array(
       'after' => '24 hours ago'
       )
  )
);

https://wordpress.stackexchange.com/questions/194352/only-display-post-if-published-in-last-24-hours

OR

 // Run the query.
 $expire_window = date('Y-m-d H:i:s', strtotime('1 day'));
 $expiring_ads = new WP_Query( array( 
        'post_type'      => 'post', 
        'posts_per_page' => 5, 
        'no_found_rows'  => true,
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
        'meta_query'     => array(
                                'relation' => 'AND',
                                array(
                                    'key' => 'start_date',
                                    'value' => $expire_window,
                                    'compare' => '<='
                                ),
                                array(
                                    'key' => 'end_date',
                                    'value' => date("Y-m-d H:i:s"),
                                    'compare' => '>=',
                                    'type' => 'DATETIME'
                                )
        )
    ));

希望这会对你有所帮助。

答案 1 :(得分:0)

您可以使用meta_query执行此操作 - https://codex.wordpress.org/Class_Reference/WP_Meta_Query

简单示例:

$start = '2011-11-31';
$end = '2011-10-01';
$args = array(
    'post_type' => 'my-event-type',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => '_my-datetime-from',
    'meta_query' => array(
        array(
            'key' => '_my-datetime-from',
            'value' => array( $start, $end ),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    )
);
// Make the query