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

回答編集履歴

1

ソースコード追記\(1\)

2017/03/09 09:08

投稿

退会済みユーザー
answer CHANGED
@@ -15,4 +15,114 @@
15
15
  return $where;
16
16
  }
17
17
  add_filter( 'getarchives_where', 'my_getarchives_where' );
18
+ ```
19
+
20
+ ###追記
21
+ 元の`wp_get_archives()`の仕様をなるべく活かすように改造したら長くなりました。
22
+ テストはあまりしていませんが、多分動くと思います。
23
+
24
+ - **`wp_get_archives()`の'post_type'パラメータが投稿タイプ名の配列を受け付けるように改造**
25
+ - **年月アーカイブページで複数の投稿タイプを表示できるようにクエリを追加**
26
+ ####アーカイブリストを表示
27
+ ```PHP
28
+ <?php
29
+ $args = array(
30
+ 'type' => 'monthly',
31
+ 'limit' => '',
32
+ 'format' => 'html',
33
+ 'before' => '',
34
+ 'after' => '',
35
+ 'show_post_count' => false,
36
+ 'echo' => 1,
37
+ 'post_type' => array('post', 'page'),
38
+ );
39
+ my_wp_get_archives( $args )
40
+ ?>
41
+ ```
42
+ ####functions.phpに記載
43
+ ```PHP
44
+ function my_query_vars( $vars ){
45
+ $vars[] = "apt";
46
+ return $vars;
47
+ }
48
+ add_filter( 'query_vars', 'my_query_vars' );
49
+
50
+ function my_pre_get_posts($query) {
51
+ if ( !is_admin() && $query->is_main_query() ) {
52
+ if ($query->is_archive) {
53
+ $apt = $query->get('apt');
54
+
55
+ if( !empty($apt) ) {
56
+ $post_type_array = explode(',', $apt);
57
+ $query->set( 'post_type', $post_type_array );
58
+ }
59
+ }
60
+ }
61
+ }
62
+ add_action( 'pre_get_posts','my_pre_get_posts' );
63
+
64
+ function my_wp_get_archives( $args = '' ) {
65
+ global $wpdb, $wp_locale;
66
+
67
+ $defaults = array(
68
+ 'type' => 'monthly', 'limit' => '',
69
+ 'format' => 'html', 'before' => '',
70
+ 'after' => '', 'show_post_count' => false,
71
+ 'echo' => 1, 'order' => 'DESC',
72
+ 'post_type' => 'post'
73
+ );
74
+
75
+ $r = wp_parse_args( $args, $defaults );
76
+
77
+ if( !is_array($r['post_type']) ) {
78
+ $r['post_type'] = array($r['post_type']);
79
+ }
80
+
81
+ foreach( $r['post_type'] as $post_type ) {
82
+ $post_type_object = get_post_type_object( $post_type );
83
+ if ( ! is_post_type_viewable( $post_type_object ) ) {
84
+ return;
85
+ }
86
+ $post_types[] = $post_type_object->name;
87
+ }
88
+
89
+ $r['post_type'] = $post_types;
90
+
91
+ if ( '' == $r['type'] ) {
92
+ $r['type'] = 'monthly';
93
+ }
94
+
95
+ if ( ! empty( $r['limit'] ) ) {
96
+ $r['limit'] = absint( $r['limit'] );
97
+ $r['limit'] = ' LIMIT ' . $r['limit'];
98
+ }
99
+
100
+ $order = strtoupper( $r['order'] );
101
+ if ( $order !== 'ASC' ) {
102
+ $order = 'DESC';
103
+ }
104
+
105
+ // this is what will separate dates on weekly archive links
106
+ $archive_week_separator = '&#8211;';
107
+
108
+ $sql_where = $wpdb->prepare("WHERE (post_type = %s", $r['post_type'][0]);
109
+ if( !(count($r['post_type'])==1) ) {
110
+ for($i=1; $i<count($r['post_type']); $i++) {
111
+ $sql_where .= $wpdb->prepare(" OR post_type = %s", $r['post_type'][$i]);
112
+ }
113
+ }
114
+ $sql_where .= ") ";
115
+
116
+ $sql_where .= "AND post_status = 'publish'";
117
+
118
+ $where = $sql_where;
119
+ $join = '';
120
+
121
+ $output = '';
122
+
123
+ $last_changed = wp_cache_get_last_changed( 'posts' );
124
+
125
+ $limit = $r['limit'];
126
+
127
+ // 文字数制限に引っかかったので、続きは別回答に記載
18
128
  ```