teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

回答に間違いがあったため、ソースコードを全面的に修正

2017/02/09 12:21

投稿

退会済みユーザー
answer CHANGED
@@ -1,38 +1,68 @@
1
- こんな感じでょうか。
1
+ **回答に間違いがあったため、ソースコードを全面的に修正ました!(2017/02/09)**
2
2
 
3
3
  以下のコードをfunctions.phpに記載してください。
4
4
  表示側は質問者様が準備されたコードのままでOKです。
5
5
  ```PHP
6
6
  function my_getarchives_where( $where, $args ){
7
- $post_type = isset( $args['post_type'] ) ? $args['post_type'] : 'post';
7
+ global $my_wp_get_archives_cat;
8
+
8
- $cat = isset( $args['cat'] ) ? $args['cat'] : '';
9
+ $my_wp_get_archives_cat = isset( $args['cat'] ) ? $args['cat'] : '';
9
- $where = "WHERE post_type = '{$post_type}' AND terms.term_id = '{$cat}' AND post_status = 'publish'";
10
10
 
11
11
  ?><pre><?php
12
+ var_dump($my_wp_get_archives_cat);
13
+ ?></pre><?php
14
+
15
+ if(!empty($my_wp_get_archives_cat)){
16
+ $where = $where . " AND terms.term_id = '" . $my_wp_get_archives_cat . "'";
17
+ }
18
+
19
+ ?><pre><?php
12
20
  var_dump($where);
13
21
  ?></pre><?php
14
22
 
15
- return $where;
23
+ return $where;
16
24
  }
17
25
  add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
18
26
 
19
27
  function my_getarchives_join($join, $args){
20
- global $wpdb;
28
+ global $wpdb;
21
-
22
- $join = " LEFT JOIN $wpdb->term_relationships as r ON $wpdb->posts.ID = r.object_ID
23
- LEFT JOIN $wpdb->term_taxonomy as t ON r.term_taxonomy_id = t.term_taxonomy_id
24
- LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id";
25
29
 
30
+ $join = " LEFT JOIN $wpdb->term_relationships as r ON $wpdb->posts.ID = r.object_ID
31
+ LEFT JOIN $wpdb->term_taxonomy as t ON r.term_taxonomy_id = t.term_taxonomy_id
32
+ LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id";
33
+
26
34
  ?><pre><?php
27
35
  var_dump($join);
28
36
  ?></pre><?php
29
37
 
30
- return $join;
38
+ return $join;
31
39
  }
32
40
  add_filter('getarchives_join', 'my_getarchives_join', 10, 2);
41
+
42
+ function my_month_link($url, $year){
43
+ global $my_wp_get_archives_cat;
44
+
45
+ ?><pre><?php
46
+ var_dump($my_wp_get_archives_cat);
47
+ ?></pre><?php
48
+
49
+ if( !empty($my_wp_get_archives_cat) ) {
50
+ $url = add_query_arg( 'cat', $my_wp_get_archives_cat, $url);
51
+ }
52
+
53
+ ?><pre><?php
54
+ var_dump($url);
55
+ ?></pre><?php
56
+
57
+ return $url;
58
+ }
59
+ add_filter('month_link', 'my_month_link', 10, 2);
33
60
  ```
34
- `wp_get_archives()`内のフィルターフック`getarchives_where`と`getarchives_join`で、
61
+ - `wp_get_archives()`内のフィルターフック`getarchives_where`と`getarchives_join`で、
35
62
  WHERE句とJOIN句を書き換えてます。
63
+ - `wp_get_archives()`内で使われている`get_month_link`内のフィルターフック`month_link`で
64
+ URLにクエリストリングを追加しています。
65
+ - グローバル変数`$my_wp_get_archives_cat`を作成し、使用しています。
36
66
 
37
67
  `?><pre><?php`から`?></pre><?php`の部分はデバッグ用なので削除して使ってください。
38
68
 
@@ -40,4 +70,5 @@
40
70
  [テンプレートタグ/wp get archives - WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/wp_get_archives)
41
71
  [general-template.php in tags/4.7/src/wp-includes – WordPress Trac](https://core.trac.wordpress.org/browser/tags/4.7/src/wp-includes/general-template.php)
42
72
  [データベース構造 - WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E6%A7%8B%E9%80%A0)
43
- [WordPress plugin Archives for a category | kwebble.com](http://kwebble.com/projects/wp-plugin-afac)
73
+ [WordPress plugin Archives for a category | kwebble.com](http://kwebble.com/projects/wp-plugin-afac)
74
+ [WordPress - wordpressにてカスタム投稿をカテゴリ指定して年別で出力したい。(65033)|teratail](https://teratail.com/questions/65033)