1

我有两个“start_date”和“end_date”的自定义字段(高级自定义字段),我想过滤两个给定日期之间的项目,这是我尝试过的:

 array(
            'key' => 'date_start',
            'value' => array($date_start_formatted, $date_end_formatted),
            'type' => 'DATE',
            'compare' => 'BETWEEN',
        ),

并且:

       array(
             'key' => 'date_start',
             'type' => 'DATE',
             'value' => $date_start_formatted,
             'compare' => '>=',
         ),

        array(
            'key' => 'date_end',
             'type' => 'DATE',
             'value' => $date_end_formatted,
             'compare' => '<=',
         ),

有没有办法找到适合这两个日期之间的所有项目日期,例如,

date_start filter = 05.05.2020 and date_end filter = 20.05.2020 

but project is date_start = 04.05.2020 and date_end is = 19.05.2020

additionally project 2 has date_start of 07.05.2020 but end date is = 22.05.2020

(basically finds all projects that have some dates between the two filtered)

出现了两个项目,因为它们的某些日期在给定的过滤器之间

4

1 回答 1

0

ACF 建议您这样做以获得两个日期之间的结果

$args  = [
   'post_type'      => 'project',
   'meta_query'     => [
      [
         'key'     => 'event_dates',
         'value'   => [$start_date, $end_date],
         'compare' => 'BETWEEN',
         'type'    => 'DATE'
      ]
   ]
];

在您的示例中,您应该将每个事件的两个日期保存在一个数组中。这是一个额外的元,但这将是做到这一点的方式。

ACF 文档: https: //support.advancedcustomfields.com/forums/topic/date-format-and-wp_query/

于 2020-12-06T21:30:43.623 回答