質問編集履歴

1

投稿後に試した書き方を追記しました。

2018/04/15 06:31

投稿

r_iida
r_iida

スコア99

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  WP_Queryでカテゴリとカスタム投稿タイプを合わせたサブループを作りたいのですが、カスタム投稿タイプにはタクソノミーがなく、カスタム投稿(cutum1, cutum2, cutum3)直下に直接記事が投稿されています。
2
2
 
3
- カテゴリは複数あり、その中の一部のカテゴリ(cate1, cate2)のみを絞り込んだ投稿と、カスタム投稿の投稿を合わせた一つのアーカイブを作りたい。
3
+ カテゴリは複数あり、その中の一部のカテゴリ(cate1, cate2)のみを絞り込んだ投稿と、カスタム投稿の投稿を合わせた一つのアーカイブを作りたいです
4
4
 
5
5
 
6
6
 
@@ -71,3 +71,153 @@
71
71
 
72
72
 
73
73
  よろしくお願いいたします。
74
+
75
+
76
+
77
+ ----------------(追記)----------------
78
+
79
+
80
+
81
+ 自分で試した別の書き方の結果です。
82
+
83
+
84
+
85
+ ※postのみ→カテゴリcate1、cate2が絞り込まれます。
86
+
87
+ ```php
88
+
89
+ $args = array(
90
+
91
+ 'post_type' => 'post',
92
+
93
+ 'tax_query' => array(
94
+
95
+ 'relation' => 'OR',
96
+
97
+ array(
98
+
99
+ 'taxonomy' => 'category',
100
+
101
+ 'field' => 'slug',
102
+
103
+ 'terms' => array( 'cate1', 'cate2' ),
104
+
105
+ 'include_children' => true
106
+
107
+ )
108
+
109
+ )
110
+
111
+ );
112
+
113
+
114
+
115
+ $the_query = new WP_Query( $args );
116
+
117
+ if ( $the_query->have_posts() ) :
118
+
119
+ while ( $the_query->have_posts() ) : $the_query->the_post();
120
+
121
+ //ループするテンプレート
122
+
123
+ endwhile;
124
+
125
+ endif;
126
+
127
+ wp_reset_postdata();
128
+
129
+
130
+
131
+ ```
132
+
133
+
134
+
135
+ ※カスタム投稿のみ→カスタム投稿、cutum1、cutum2、cutum3のみ表示されます。
136
+
137
+ ```php
138
+
139
+ $args = array(
140
+
141
+ 'post_type' => array('cutum1','cutum2','cutum3'),
142
+
143
+ );
144
+
145
+
146
+
147
+ $the_query = new WP_Query( $args );
148
+
149
+ if ( $the_query->have_posts() ) :
150
+
151
+ while ( $the_query->have_posts() ) : $the_query->the_post();
152
+
153
+ //ループするテンプレート
154
+
155
+ endwhile;
156
+
157
+ endif;
158
+
159
+ wp_reset_postdata();
160
+
161
+
162
+
163
+ ```
164
+
165
+
166
+
167
+ ※カテゴリを$args1、カスタム投稿を$args2に分割
168
+
169
+ →カテゴリが絞り込まれないまま表示されて、カスタム投稿は表示されないようでした。
170
+
171
+ ```php
172
+
173
+ $args1 = array(
174
+
175
+ 'post_type' => 'post',
176
+
177
+ 'tax_query' => array(
178
+
179
+ 'relation' => 'OR',
180
+
181
+ array(
182
+
183
+ 'taxonomy' => 'category',
184
+
185
+ 'field' => 'slug',
186
+
187
+ 'terms' => array( 'cate1', 'cate2' ),
188
+
189
+ 'include_children' => true
190
+
191
+ )
192
+
193
+ )
194
+
195
+ );
196
+
197
+
198
+
199
+ $args2 = array(
200
+
201
+ 'post_type' => array('cutum1','cutum2','cutum3'),
202
+
203
+ );
204
+
205
+
206
+
207
+ $the_query = new WP_Query( $args1 || $args2 );
208
+
209
+ if ( $the_query->have_posts() ) :
210
+
211
+ while ( $the_query->have_posts() ) : $the_query->the_post();
212
+
213
+ //ループするテンプレート
214
+
215
+ endwhile;
216
+
217
+ endif;
218
+
219
+ wp_reset_postdata();
220
+
221
+
222
+
223
+ ```