質問編集履歴
3
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
Wordpressの人気記事ランキングのwordpress popular postsですが、
|
2
|
+
|
1
|
-
3日前~2日前
|
3
|
+
集計期間をオリジナルで例えば3日前~2日前とかの期間が知りたいのですがどうすれば良いのでしょうか。
|
2
4
|
|
3
5
|
申し訳ありません。教えてください。
|
4
6
|
|
2
更新情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
-
申し訳ございません。
|
47
|
+
申し訳ございません。追加情報を記載します。お許しください。Wordpressの人気記事ランキングなのですが・・。
|
48
48
|
|
49
49
|
|
50
50
|
|
1
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -41,3 +41,557 @@
|
|
41
41
|
break;
|
42
42
|
|
43
43
|
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
申し訳ございません。
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
protected function _query_posts($instance) {
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
global $wpdb;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
// parse instance values
|
60
|
+
|
61
|
+
$instance = $this->__merge_array_r(
|
62
|
+
|
63
|
+
$this->defaults,
|
64
|
+
|
65
|
+
$instance
|
66
|
+
|
67
|
+
);
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
$prefix = $wpdb->prefix . "popularposts";
|
72
|
+
|
73
|
+
$fields = "p.ID AS 'id', p.post_title AS 'title', p.post_date AS 'date', p.post_author AS 'uid'";
|
74
|
+
|
75
|
+
$from = "";
|
76
|
+
|
77
|
+
$where = "WHERE 1 = 1";
|
78
|
+
|
79
|
+
$orderby = "";
|
80
|
+
|
81
|
+
$groupby = "";
|
82
|
+
|
83
|
+
$limit = "LIMIT {$instance['limit']}";
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
$post_types = "";
|
88
|
+
|
89
|
+
$pids = "";
|
90
|
+
|
91
|
+
$cats = "";
|
92
|
+
|
93
|
+
$authors = "";
|
94
|
+
|
95
|
+
$content = "";
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
$now = $this->__now();
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// post filters
|
104
|
+
|
105
|
+
// * freshness - get posts published within the selected time range only
|
106
|
+
|
107
|
+
if ( $instance['freshness'] ) {
|
108
|
+
|
109
|
+
switch( $instance['range'] ){
|
110
|
+
|
111
|
+
case "daily":
|
112
|
+
|
113
|
+
$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 DAY) ";
|
114
|
+
|
115
|
+
break;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
case "weekly":
|
120
|
+
|
121
|
+
$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 WEEK) ";
|
122
|
+
|
123
|
+
break;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
case "monthly":
|
128
|
+
|
129
|
+
$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 MONTH) ";
|
130
|
+
|
131
|
+
break;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
default:
|
136
|
+
|
137
|
+
$where .= "";
|
138
|
+
|
139
|
+
break;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// * post types - based on code seen at https://github.com/williamsba/WordPress-Popular-Posts-with-Custom-Post-Type-Support
|
148
|
+
|
149
|
+
$types = explode(",", $instance['post_type']);
|
150
|
+
|
151
|
+
$sql_post_types = "";
|
152
|
+
|
153
|
+
$join_cats = true;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// if we're getting just pages, why join the categories table?
|
158
|
+
|
159
|
+
if ( 'page' == strtolower($instance['post_type']) ) {
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
$join_cats = false;
|
164
|
+
|
165
|
+
$where .= " AND p.post_type = '{$instance['post_type']}'";
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
// we're listing other custom type(s)
|
172
|
+
|
173
|
+
else {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
if ( count($types) > 1 ) {
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
foreach ( $types as $post_type ) {
|
182
|
+
|
183
|
+
$post_type = trim($post_type); // required in case user places whitespace between commas
|
184
|
+
|
185
|
+
$sql_post_types .= "'{$post_type}',";
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
$sql_post_types = rtrim( $sql_post_types, ",");
|
192
|
+
|
193
|
+
$where .= " AND p.post_type IN({$sql_post_types})";
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
} else {
|
198
|
+
|
199
|
+
$where .= " AND p.post_type = '{$instance['post_type']}'";
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
// * posts exclusion
|
210
|
+
|
211
|
+
if ( !empty($instance['pid']) ) {
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
$ath = explode(",", $instance['pid']);
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
$where .= ( count($ath) > 1 )
|
220
|
+
|
221
|
+
? " AND p.ID NOT IN({$instance['pid']})"
|
222
|
+
|
223
|
+
: " AND p.ID <> '{$instance['pid']}'";
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// * categories
|
232
|
+
|
233
|
+
if ( !empty($instance['cat']) && $join_cats ) {
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
$cat_ids = explode(",", $instance['cat']);
|
238
|
+
|
239
|
+
$in = array();
|
240
|
+
|
241
|
+
$out = array();
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
for ($i=0; $i < count($cat_ids); $i++) {
|
246
|
+
|
247
|
+
if ($cat_ids[$i] >= 0)
|
248
|
+
|
249
|
+
$in[] = $cat_ids[$i];
|
250
|
+
|
251
|
+
else
|
252
|
+
|
253
|
+
$out[] = $cat_ids[$i];
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
$in_cats = implode(",", $in);
|
260
|
+
|
261
|
+
$out_cats = implode(",", $out);
|
262
|
+
|
263
|
+
$out_cats = preg_replace( '|[^0-9,]|', '', $out_cats );
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
if ($in_cats != "" && $out_cats == "") { // get posts from from given cats only
|
268
|
+
|
269
|
+
$where .= " AND p.ID IN (
|
270
|
+
|
271
|
+
SELECT object_id
|
272
|
+
|
273
|
+
FROM {$wpdb->term_relationships} AS r
|
274
|
+
|
275
|
+
JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
|
276
|
+
|
277
|
+
WHERE x.taxonomy = 'category' AND x.term_id IN({$in_cats})
|
278
|
+
|
279
|
+
)";
|
280
|
+
|
281
|
+
} else if ($in_cats == "" && $out_cats != "") { // exclude posts from given cats only
|
282
|
+
|
283
|
+
$where .= " AND p.ID NOT IN (
|
284
|
+
|
285
|
+
SELECT object_id
|
286
|
+
|
287
|
+
FROM {$wpdb->term_relationships} AS r
|
288
|
+
|
289
|
+
JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
|
290
|
+
|
291
|
+
WHERE x.taxonomy = 'category' AND x.term_id IN({$out_cats})
|
292
|
+
|
293
|
+
)";
|
294
|
+
|
295
|
+
} else { // mixed
|
296
|
+
|
297
|
+
$where .= " AND p.ID IN (
|
298
|
+
|
299
|
+
SELECT object_id
|
300
|
+
|
301
|
+
FROM {$wpdb->term_relationships} AS r
|
302
|
+
|
303
|
+
JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
|
304
|
+
|
305
|
+
WHERE x.taxonomy = 'category' AND x.term_id IN({$in_cats}) AND x.term_id NOT IN({$out_cats})
|
306
|
+
|
307
|
+
) ";
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
// * authors
|
318
|
+
|
319
|
+
if ( !empty($instance['author']) ) {
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
$ath = explode(",", $instance['author']);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
$where .= ( count($ath) > 1 )
|
328
|
+
|
329
|
+
? " AND p.post_author IN({$instance['author']})"
|
330
|
+
|
331
|
+
: " AND p.post_author = '{$instance['author']}'";
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
// All-time range
|
340
|
+
|
341
|
+
if ( "all" == $instance['range'] ) {
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
$fields .= ", p.comment_count AS 'comment_count'";
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
// order by comments
|
350
|
+
|
351
|
+
if ( "comments" == $instance['order_by'] ) {
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
$from = "{$wpdb->posts} p";
|
356
|
+
|
357
|
+
$where .= " AND p.comment_count > 0 ";
|
358
|
+
|
359
|
+
$orderby = " ORDER BY p.comment_count DESC";
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
// get views, too
|
364
|
+
|
365
|
+
if ( $instance['stats_tag']['views'] ) {
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
$fields .= ", IFNULL(v.pageviews, 0) AS 'pageviews'";
|
370
|
+
|
371
|
+
$from .= " LEFT JOIN {$prefix}data v ON p.ID = v.postid";
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
// order by (avg) views
|
382
|
+
|
383
|
+
else {
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
$from = "{$prefix}data v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID";
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
// order by views
|
392
|
+
|
393
|
+
if ( "views" == $instance['order_by'] ) {
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
$fields .= ", v.pageviews AS 'pageviews'";
|
398
|
+
|
399
|
+
$orderby = "ORDER BY pageviews DESC";
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
// order by avg views
|
406
|
+
|
407
|
+
elseif ( "avg" == $instance['order_by'] ) {
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
$fields .= ", ( v.pageviews/(IF ( DATEDIFF('{$now}', MIN(v.day)) > 0, DATEDIFF('{$now}', MIN(v.day)), 1) ) ) AS 'avg_views'";
|
412
|
+
|
413
|
+
$groupby = "GROUP BY v.postid";
|
414
|
+
|
415
|
+
$orderby = "ORDER BY avg_views DESC";
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
} else { // CUSTOM RANGE
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
$interval = "";
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
switch( $instance['range'] ){
|
436
|
+
|
437
|
+
case "daily":
|
438
|
+
|
439
|
+
$interval = "1 DAY";
|
440
|
+
|
441
|
+
break;
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
case "weekly":
|
446
|
+
|
447
|
+
$interval = "1 WEEK";
|
448
|
+
|
449
|
+
break;
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
case "monthly":
|
454
|
+
|
455
|
+
$interval = "1 MONTH";
|
456
|
+
|
457
|
+
break;
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
default:
|
462
|
+
|
463
|
+
$interval = "1 DAY";
|
464
|
+
|
465
|
+
break;
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
// order by comments
|
472
|
+
|
473
|
+
if ( "comments" == $instance['order_by'] ) {
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
$fields .= ", COUNT(c.comment_post_ID) AS 'comment_count'";
|
478
|
+
|
479
|
+
$from = "{$wpdb->comments} c LEFT JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID";
|
480
|
+
|
481
|
+
$where .= " AND c.comment_date_gmt > DATE_SUB('{$now}', INTERVAL {$interval}) AND c.comment_approved = 1 ";
|
482
|
+
|
483
|
+
$groupby = "GROUP BY c.comment_post_ID";
|
484
|
+
|
485
|
+
$orderby = "ORDER BY comment_count DESC";
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
if ( $instance['stats_tag']['views'] ) { // get views, too
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
$fields .= ", IFNULL(v.pageviews, 0) AS 'pageviews'";
|
494
|
+
|
495
|
+
$from .= " LEFT JOIN (SELECT postid, SUM(pageviews) AS pageviews FROM {$prefix}summary WHERE last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) GROUP BY postid) v ON p.ID = v.postid";
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
// ordered by views / avg
|
506
|
+
|
507
|
+
else {
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
$from = "{$prefix}summary v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID";
|
512
|
+
|
513
|
+
$where .= " AND v.last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) ";
|
514
|
+
|
515
|
+
$groupby = "GROUP BY v.postid";
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
// ordered by views
|
520
|
+
|
521
|
+
if ( "views" == $instance['order_by'] ) {
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
$fields .= ", SUM(v.pageviews) AS 'pageviews'";
|
526
|
+
|
527
|
+
$orderby = "ORDER BY pageviews DESC";
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
}
|
532
|
+
|
533
|
+
// ordered by avg views
|
534
|
+
|
535
|
+
elseif ( "avg" == $instance['order_by'] ) {
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
$fields .= ", ( SUM(v.pageviews)/(IF ( DATEDIFF('{$now}', DATE_SUB('{$now}', INTERVAL {$interval})) > 0, DATEDIFF('{$now}', DATE_SUB('{$now}', INTERVAL {$interval})), 1) ) ) AS 'avg_views' ";
|
540
|
+
|
541
|
+
$orderby = "ORDER BY avg_views DESC";
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
// get comments, too
|
550
|
+
|
551
|
+
if ( $instance['stats_tag']['comment_count'] ) {
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
$fields .= ", IFNULL(c.comment_count, 0) AS 'comment_count'";
|
556
|
+
|
557
|
+
$from .= " LEFT JOIN (SELECT comment_post_ID, COUNT(comment_post_ID) AS 'comment_count' FROM {$wpdb->comments} WHERE comment_date_gmt > DATE_SUB('{$now}', INTERVAL {$interval}) AND comment_approved = 1 GROUP BY comment_post_ID) c ON p.ID = c.comment_post_ID";
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
}
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
}
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
// List only published, non password-protected posts
|
574
|
+
|
575
|
+
$where .= " AND p.post_password = '' AND p.post_status = 'publish'";
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
// Build query
|
580
|
+
|
581
|
+
$query = "SELECT {$fields} FROM {$from} {$where} {$groupby} {$orderby} {$limit};";
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
$this->__debug( $query );
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
$result = $wpdb->get_results($query);
|
590
|
+
|
591
|
+
|
592
|
+
|
593
|
+
return apply_filters( 'wpp_query_posts', $result, $instance );
|
594
|
+
|
595
|
+
|
596
|
+
|
597
|
+
} // end query_posts
|