[英]wordpress: only show posts from today or the future
I am running this query to print out my posts. 我正在运行此查询以打印我的帖子。 It works but I want to add a parameter that tells the system to only display posts that are from today or will be published in the future! 它有效,但我想添加一个参数,告诉系统只显示今天发布的帖子或将来发布的帖子!
Here is the query: 这是查询:
$today = getdate();
$year=$today["year"];
$month=$today["mon"];
$day=$today["mday"];
query_posts( $query_string.'order=ASC' .
'&post.status=future,publish' .
'&year='.$year .
'&monthnum='.$month
);
I tried to do something like &post.date = <= $today
but that didn't work. 我尝试做了类似&post.date = <= $today
但是没有用。
please, can anyone tell me how to do it? 拜托,谁能告诉我怎么做?
My idea is to tell the query to only show posts with a publishing-date that is today or "less" than today. 我的想法是告诉查询只显示发布日期是今天或“少于”今天的帖子。 That's why the " <= "
. 这就是" <= "
的原因。
$future_args = array(
'post_status' => 'future'
// possibly further query arguments
);
$today = getdate();
$today_args = array(
'year' => $today['year'],
'monthnum' => $today['mon'],
'day' => $today['mday']
// possibly further query arguments
);
$future_query = new WP_Query( $future_args );
$today_query = new WP_Query( $today_args );
while ( $today_query->have_posts() ) :
$today_query->the_post();
// echo something
endwhile;
wp_reset_postdata();
while ( $future_query->have_posts() ) :
$future_query->the_post();
// echo something
endwhile;
wp_reset_postdata();
That ought to do it. 应该这样做。
See the codex article on the WP_Query class for reference. 请参阅WP_Query类上的codex文章以供参考。
Have you all found that the future posts are only viewable to admins or authors? 您是否都发现未来的帖子只能供管理员或作者查看? I wanted to list all future post titles to non members and the query would always return 0 posts when i queried for 'future', unless i was logged in as an admin. 我想列出所有未来的帖子标题给非成员,当我查询'future'时,查询总是返回0个帖子,除非我以管理员身份登录。
Doing a var dump of the returned array of 0 results i found various settings that were un set and one suppress_filters caught my eye so setting it to 1 (true) i got the result i wanted. 执行返回的0数组的var转储我发现各种设置未设置,一个suppress_filters引起了我的注意,因此将其设置为1(true)我得到了我想要的结果。
So anyone having this problem i found the solution, its just 1 extra setting in the query 'suppress_filters' => '1' 所以有任何人遇到这个问题我找到了解决方案,在查询中只有一个额外的设置'suppress_filters'=>'1'
so for example 所以例如
$args = array (
'post_status' => 'future',
'suppress_filters' => '1'
);
$query = new WP_Query($args);
After searching via google for hours i could not find this simple solution. 经过谷歌搜索几个小时后,我找不到这个简单的解决方案。 But there it is hope it helps someone else. 但它希望它可以帮助别人。
This approach worked for me, using date_query parameters (available since WP 3.7). 这种方法对我有用,使用date_query参数(自WP 3.7起可用)。
$args = array(
'post_status' => 'publish,future',
'date_query' => array(
array(
'after' => date('Y-m-d'),
'inclusive' => true,
)
),
);
$query = new WP_Query($args);
It queries all posts from the whole current day (before or after the current time) and into the future. 它查询整个当天(当前时间之前或之后)和未来的所有帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.