回答編集履歴
2
追記
test
CHANGED
@@ -128,4 +128,4 @@
|
|
128
128
|
|
129
129
|
```
|
130
130
|
|
131
|
-
|
131
|
+
ただ、「WP Most Popular」プラグインは**最終更新が一年前**なので、個人的には使用はおすすめしません。
|
1
コード例追記\(2016/12/11\)
test
CHANGED
@@ -13,3 +13,119 @@
|
|
13
13
|
参考
|
14
14
|
|
15
15
|
[関数リファレンス/wpdb Class - WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wpdb_Class)
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
###コード例追記(2016/12/11)
|
20
|
+
|
21
|
+
**実用にはたえないので、ご参考までに**というレベルですが、
|
22
|
+
|
23
|
+
「WP Most Popular」のPV数を月ごとに集計、表示するショートコードを作りました。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
`[mycount year=2016 month=10]`
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```PHP
|
32
|
+
|
33
|
+
function my_most_popular_count($atts) {
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
extract(shortcode_atts(array(
|
38
|
+
|
39
|
+
'year' => 2016,
|
40
|
+
|
41
|
+
'month' => 12,
|
42
|
+
|
43
|
+
), $atts));
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
// most_popularテーブルから該当の年月を含むデータを取得
|
48
|
+
|
49
|
+
global $wpdb;
|
50
|
+
|
51
|
+
$tablename = $wpdb->prefix . 'most_popular';
|
52
|
+
|
53
|
+
$date_ym = date('Y-m', strtotime("{$year}-{$month}"));
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
$results = $wpdb->get_results("SELECT post_id, raw_stats FROM {$tablename} WHERE raw_stats LIKE '%{$date_ym}%'");
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
if( !empty($results) ) {
|
62
|
+
|
63
|
+
// データがあったら
|
64
|
+
|
65
|
+
$list = array();
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
// 各投稿の指定月のPV数を集計
|
70
|
+
|
71
|
+
foreach($results as $result) {
|
72
|
+
|
73
|
+
$data = unserialize($result->raw_stats);
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
$val = 0;
|
78
|
+
|
79
|
+
for( $day=1; $day<=31; $day++ ) {
|
80
|
+
|
81
|
+
$date_ymd = date('Y-m-d', strtotime("{$date_ym}-{$day}"));
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
$val += $data["{$date_ymd}"];
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
$list[$result->post_id] = $val;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// 結果をtableで整形
|
98
|
+
|
99
|
+
foreach($list as $key=>$val) {
|
100
|
+
|
101
|
+
$ret .= '<tr><td>' . $key . '</td><td>' . $val . '</td></tr>';
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
$ret = '<table><tr><th>投稿ID</th><th>PV数</th></tr>' . $ret . '</table>';
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
else {
|
112
|
+
|
113
|
+
// データがなかったら
|
114
|
+
|
115
|
+
$ret = 'no data<br>';
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
return $ret;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
add_shortcode('mycount', 'my_most_popular_count');
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
|