我有一个工作查询,使用pre_get_posts
使用status
post格式,仅在使用UNIX时间戳的日期范围内显示这些帖子。唯一的问题是,即使帖子超出范围,它仍然会被查询。
我只是想知道是否有更好的方法使用自定义字段查询帖子,该字段使用的
代码:
$args = array(\'status_message\' => \'alerts\');
$status_alert = new WP_Query( $args );
$notice_startend = false;
$notice_start = false;
$duplicates = [];
while ( $status_alert->have_posts() ) :
$status_alert->the_post();
if ((get_field(\'status_type\') == \'notice\') && get_field(\'status_start_date\') < date( \'U\', current_time( \'timestamp\', 0 ) ) && (get_field(\'status_end_date\')==\'\')):
$notice_start = true;
$duplicates[] = get_the_ID();
endif;
if ((get_field(\'status_type\') == \'notice\') && get_field(\'status_start_date\') < date( \'U\', current_time( \'timestamp\', 0 ) ) && get_field(\'status_end_date\') > date( \'U\', current_time( \'timestamp\', 0 ) )):
$notice_startend = true;
$duplicates[] = get_the_ID();
endif;
endwhile;
if (($notice_start == true) or ($notice_startend == true)):
<section class="notice-top">
foreach ($duplicates as $postID) :
if (get_field(\'status_type\' , $postID) == \'notice\'):
<div class="notice-wrap">
if (get_field(\'status_dealbox\', $postID)):
<div class="box-notice-left"><?php echo get_field(\'status_dealbox\', $postID);?></div>
endif;
<div class="noc-notice-msg"><?php echo (get_post_field(\'post_content\', $postID));?></div>
</div>
endif;
endforeach;
endif;
最合适的回答,由SO网友:Daniel Fonda 整理而成
像这样的怎么样?
$args = array(
\'date_query\' => array(
array(
\'before\' => array(
\'year\' => 2013,
\'month\' => 2,
\'day\' => 28,
),
\'inclusive\' => true,
),
),
\'posts_per_page\' => -1,
);
$query = new WP_Query( $args );
显然,您可以通过变量设置确切的日期。
更多信息:https://codex.wordpress.org/Class_Reference/WP_Query