-
Notifications
You must be signed in to change notification settings - Fork 29
/
post-count-month.php
137 lines (116 loc) · 3.27 KB
/
post-count-month.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
Plugin Name: Post Count by Month
Plugin URI: http://wordpress.stackexchange.com/q/60859/6035
Description: A short code that outputs a list of month names and post counts
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('init', 'wpse60859_register_shortcode');
/**
* Registers the shortcode
*
* @uses add_shortcode
*/
function wpse60859_register_shortcode()
{
add_shortcode(
'posts_per_month',
'wpse60859_shortcode_cb'
);
add_shortcode(
'posts_per_month_last',
'wpse60859_shortcode_alt_cb'
);
}
/**
* The shortcode callback function.
*
* Usage:
* [posts_per_month year="2012"]
*
* @uses shortcode_atts
* @uses date_i18n
*/
function wpse60859_shortcode_cb($args)
{
global $wpdb;
$args = shortcode_atts(array(
'year' => false
), $args);
$year = absint($args['year']);
// year is a no go? bail.
if(!$year)
return '';
$res = $wpdb->get_results($wpdb->prepare(
"SELECT MONTH(post_date) AS post_month, count(ID) AS post_count from " .
"{$wpdb->posts} WHERE post_status = 'publish' AND YEAR(post_date) = %d " .
"GROUP BY post_month;", $year
), OBJECT_K);
// We didn't get any results. Something might be wrong?
if(!$res)
return '';
// build the display
$out = '<ul>';
foreach(range(1, 12) as $m)
{
$month = date_i18n('F', mktime(0, 0, 0, $m, 1));
$out .= sprintf(
'<li>%s %d</li>',
$month,
isset($res[$m]) ? $res[$m]->post_count : 0
);
}
$out .= '</ul>';
return $out;
}
/**
* Callback for displaying the last twelve months of posts
*
* @uses $wpdb
*/
function wpse60859_shortcode_alt_cb()
{
global $wpdb;
$res = $wpdb->get_results(
"SELECT MONTH(post_date) as post_month, COUNT(ID) as post_count " .
"FROM {$wpdb->posts} " .
"WHERE post_date BETWEEN DATE_SUB(NOW(), INTERVAL 12 MONTH) AND NOW() " .
"AND post_status = 'publish' " .
"GROUP BY post_month ORDER BY post_date ASC", OBJECT_K
);
print_r($res);
$cur = absint(date('n'));
if($cur > 1)
{
$looper = array_merge(range($cur, 12), range(1, $cur-1));
}
else
{
$looper = range(1, 12);
}
$out = '<ul>';
foreach($looper as $m)
{
$month = date_i18n('F', mktime(0, 0, 0, $m, 1));
$out .= sprintf(
'<li>%s %d</li>',
$month,
isset($res[$m]) ? $res[$m]->post_count : 0
);
}
$out .= '</ul>';
return $out;
}