简体   繁体   English

如何在WordPress中显示给定时间范围内的帖子数

[英]How to display number of posts for a given time frame in wordpress

I was hoping to get some ideas as how I can get a number of posts that have been posted on our blog BUT within a certain date range. 我希望能得到一些想法,因为我将如何获得在特定日期范围内已发布在博客BUT上的大量帖子。 I know how to get total number but need this extra functionality. 我知道如何获取总数,但需要此额外功能。

This is what I have for getting total number of posts: 这就是我要获得的职位总数:

$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

Then I just echo out where ever I want to display that number 然后我回显我要显示该数字的位置

<?php if ( is_page('wordpress-numbers')) {
echo "<strong>".$numposts.' posts have been published since August 12, 2009'."</strong>";
}
?>

For example I want to know the number of posts in the last 7 days. 例如,我想知道过去7天的帖子数。 Maybe use a datepicker of sorts? 也许使用日期选择器?

Thanks, 谢谢,

Matt 马特

Sure, you could do it by setting a date range in a custom query : 当然,您可以通过在自定义查询中设置日期范围来做到这一点:

$today = date("Y-m-d");
$sevenDaysAgo = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));

$querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts,
    WHERE wposts.post_date BETWEEN '" . $today . "' AND '" . $sevenDaysAgo . "' 
    AND wposts.post_type = 'post'
    ORDER BY wposts.post_date DESC
";

Okay thanks to Pat he got me thinking here and I came up with this: 好的,感谢Pat,他让我在这里思考,然后我想到了:

$querystr = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY) LIMIT 0, 30");
if (0 < $querystr) $querystr = number_format($querystr);

Then I will just echo out the result: 然后,我将回显结果:

echo "<strong>".$querystr.' posts have been published in the last 7 days'."</strong>";

The next thing I would like to do is add the ability to choose the start date then show results for those 7days. 我想做的下一件事是添加选择开始日期的功能,然后显示这7天的结果。

Thanks again Pat 再次感谢帕特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM