使用meta query. 因为ACF将日期存储为Ymd
您可以将该值视为一个数字:
$date_query = array(
\'key\' => \'start_date\', // ACF date field name
\'type\' => \'NUMERIC\',
\'value\' => \'20200101\',
\'compare\' => \'<=\', // All posts with start date before/on January 1st 2020
);
$args[\'meta_query\'] = array( $date_query );
任何典型比较,例如。
=
,
!=
,
>
,
>=
,
<
,
<=
可用于的值
compare
.
或者,您需要一个日期范围,例如,要获得2019年12月的所有帖子:
$date_query = array(
\'key\' => \'start_date\',
\'type\' => \'NUMERIC\',
\'value\' => array( \'20191201\' /* Start date */, \'20191231\' /* End date */ ),
\'compare\' => \'BETWEEN\',
);
下面是一个(未经测试的)问题示例:
// Grab the current date for the set timezone
$currentDate = new DateTimeImmutable( current_time( \'mysql\', true /* GMT */ ), new DateTimeZone( \'America/Edmonton\' ) );
// Add fourteen days to the current date
// Use this variable in a loop to increment the total number of visible posts by one every 14 days
$datePlusFourteen = $currentDate->add( new DateInterval( \'P14D\' ) );
$args = array(
\'post_type\' => \'sendouts\',
\'cat\' => \'101\',
\'order\' => \'ASC\',
\'orderby\' => \'p\',
\'meta_query\' => array(
array(
\'key\' => \'sendout_start_date\',
\'type\' => \'NUMERIC\',
\'value\' => array( $currentDate->format( \'Ymd\' ), $datePlusFourteen->format( \'Ymd\' ) ),
\'compare\' => \'BETWEEN\',
),
),
);
$query = new WP_Query( $args );