{"id":330,"date":"2006-10-14T12:16:16","date_gmt":"2006-10-14T16:16:16","guid":{"rendered":"http:\/\/jclark.org\/weblog\/2006\/10\/14\/counting-posts\/"},"modified":"2006-10-14T12:16:16","modified_gmt":"2006-10-14T16:16:16","slug":"counting-posts","status":"publish","type":"post","link":"https:\/\/jclark.org\/weblog\/2006\/10\/14\/counting-posts\/","title":{"rendered":"Counting Posts"},"content":{"rendered":"<p>Daniel at <a href=\"http:\/\/thewebdesignjournal.com\/\">The Web Design Journal<\/a> emailed me to ask about the post count block that appears in my sidebar.  For example:<\/p>\n<blockquote>\n<p>There are 2 posts in the last 30 days, and 323 total posts.<\/p>\n<\/blockquote>\n<p>There are a number of ways to get the total post count. The last 30 days post count is a feature I wanted to add when I switched to Wordpress and designed this theme.  After searching for an existing solution, I ended up rolling my own.  For reasons I don&#8217;t recall, I also wrote my own function for getting the total post count.  I had meant to post it here, but had forgotten about it.  <\/p>\n<p>Instead of designing this as a Wordpress plugin, I added it directly to template.  In my sidebar template, I added the following:<\/p>\n<p>There are  posts in the last 30 days, \n        and  total posts.  \n        &lt;a class=&quot;sidelink&quot; href=&quot;\/archive\/&#8221;&gt;Archives \u00bb<\/a><\/p>\n<p>The special bits here are the calls to <code>get_post_count_inlast()<\/code> and <code>get_post_count()<\/code>, which are simple PHP functions I wrote to query the Wordpress MySQL database.  I added these functions to <code>functions.php<\/code> in the theme directory for my theme.  I&#8217;m not sure if all Wordpress theme include this file, but the default theme, on which I based my theme, already provided this file.  In this file, I added the code for the two new functions:<\/p>\n<pre><code>function get_post_count() {\n    global $wpdb;\n    return $wpdb-&gt;get_var(&#039;select count(*) from wp_posts where post_status = &quot;publish&quot;&#039;);\n}\n\nfunction get_post_count_inlast($days=30) {\n    global $wpdb;\n    return $wpdb-&gt;get_var(&quot;select count(*) from wp_posts where post_status = &#039;publish&#039; and DATEDIFF(CURRENT_DATE, POST_DATE) &lt;= $days&quot;);\n}<\/code><\/pre>\n<p>I&#8217;m no PHP expert, I&#8217;ve picked up just enough to make the tweaks I wanted to my templates, but I believe you could even include the function definitions directly in the <code>sidebar.php<\/code> file, as long as they&#8217;re inside a <code>&lt;?php  ...  ?&gt;<\/code> tag.<\/p>\n<p>Hope someone finds this useful.  If you decide to add this to your site, please drop me a comment, and be sure to fill in your site URL.<\/p>\n<p>(The source code in this post is released under the <a href=\"http:\/\/codex.wordpress.org\/GPL\">GNU GPL<\/a> (just like Wordpress), and comes with no warranty of any kind, express or implied.)<\/p>","protected":false},"excerpt":{"rendered":"<p>Daniel at The Web Design Journal emailed me to ask about the post count block that appears in my sidebar. For example: There are 2 posts in the last 30 days, and 323 total posts. There are a number of ways to get the total post count. The last 30 days post count is a [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[61,34],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-themes","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":0,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"wp:attachment":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}