我在这里尝试的缺点是:我只想根据存储在自定义字段中的日期显示一篇文章。我只想显示符合两个条件的帖子1:日期必须尚未发生,即必须大于当前日期2:要显示的帖子日期必须是所有可能帖子中最接近当前日期的。这是下一个日期。
我的问题是,当我要比较一篇文章和另一篇文章,然后使用WordPress循环只显示适当的文章时,我不知道从哪里开始。我想我需要使用for循环遍历每个帖子并进行比较,然后运行一个新的查询来显示我的一篇帖子。
太长,读不下去了我可以使用forloop来检查WP\\u查询吗?如何访问其中的帖子信息?
这是我的头脑风暴代码
<?php
$current_date = strtotime(date( \'F j, g:i a\' ));
$soonest_date = 0;
$post_to_display = 0;
$posts = new WP_query($args); //will this work? Can I run a for loop over this?
//loop through all posts (imagining the posts are stored in a $posts array)
foreach( $posts as $post ) :
$post_date = srttotime(get_field(\'date\')); //Does this need to be $post[\'date\']? or something like that?
//Check if $soonest_date is set, if not then set it as the current post\'s date
if(! $soonest_date){
$soonest_date = $post_date;
}
//Check to see if the current post\'s date is larger than todays date and is less then the current soonest date
if($current_date < $post_date < $soonest_date ){
//if it is then this is the new soonest date and would be the one to be displayd if no smaller is found
$soonest_date = $post_date;
$post_to_display = the_ID(); // Again I think this might need to be something like $post[\'ID\']?
}
?>
提前感谢!我试图尽可能清楚地说明这一点,但我很困惑:D
最合适的回答,由SO网友:Faham Shaikh 整理而成
您的查询应该如下所示:
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'meta_key\' => \'<ACF FIELD KEY HERE>\',
\'meta_value\' => date( "Ymd" ),
\'meta_compare\' => \'>\',
\'meta_query\' => array(
\'key\' => \'<ACF FIELD KEY HERE>\',
\'value\' => date( "Ymd" ),
\'compare\' => \'>\'
),
\'orderby\' => \'meta_value_datetime\',
\'order\' => \'ASC\',
);
$queryPosts = new WP_query($args);
if($queryPosts->have_posts) {
while($queryPosts->have_posts) {
$queryPosts->the_post();
the_title();
}
}