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

質問編集履歴

1

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

2018/04/15 06:31

投稿

r_iida
r_iida

スコア99

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  WP_Queryでカテゴリとカスタム投稿タイプを合わせたサブループを作りたいのですが、カスタム投稿タイプにはタクソノミーがなく、カスタム投稿(cutum1, cutum2, cutum3)直下に直接記事が投稿されています。
2
- カテゴリは複数あり、その中の一部のカテゴリ(cate1, cate2)のみを絞り込んだ投稿と、カスタム投稿の投稿を合わせた一つのアーカイブを作りたい。
2
+ カテゴリは複数あり、その中の一部のカテゴリ(cate1, cate2)のみを絞り込んだ投稿と、カスタム投稿の投稿を合わせた一つのアーカイブを作りたいです
3
3
 
4
4
  ```php
5
5
  $args = array(
@@ -34,4 +34,79 @@
34
34
  [WordPressでカスタムタクソノミーの投稿を絞込み](https://ghweb.info/post-3685.html)
35
35
  の「複数のカスタム投稿タイプと属する複数のタームを表示」
36
36
 
37
- よろしくお願いいたします。
37
+ よろしくお願いいたします。
38
+
39
+ ----------------(追記)----------------
40
+
41
+ 自分で試した別の書き方の結果です。
42
+
43
+ ※postのみ→カテゴリcate1、cate2が絞り込まれます。
44
+ ```php
45
+ $args = array(
46
+ 'post_type' => 'post',
47
+ 'tax_query' => array(
48
+ 'relation' => 'OR',
49
+ array(
50
+ 'taxonomy' => 'category',
51
+ 'field' => 'slug',
52
+ 'terms' => array( 'cate1', 'cate2' ),
53
+ 'include_children' => true
54
+ )
55
+ )
56
+ );
57
+
58
+ $the_query = new WP_Query( $args );
59
+ if ( $the_query->have_posts() ) :
60
+ while ( $the_query->have_posts() ) : $the_query->the_post();
61
+ //ループするテンプレート
62
+ endwhile;
63
+ endif;
64
+ wp_reset_postdata();
65
+
66
+ ```
67
+
68
+ ※カスタム投稿のみ→カスタム投稿、cutum1、cutum2、cutum3のみ表示されます。
69
+ ```php
70
+ $args = array(
71
+ 'post_type' => array('cutum1','cutum2','cutum3'),
72
+ );
73
+
74
+ $the_query = new WP_Query( $args );
75
+ if ( $the_query->have_posts() ) :
76
+ while ( $the_query->have_posts() ) : $the_query->the_post();
77
+ //ループするテンプレート
78
+ endwhile;
79
+ endif;
80
+ wp_reset_postdata();
81
+
82
+ ```
83
+
84
+ ※カテゴリを$args1、カスタム投稿を$args2に分割
85
+ →カテゴリが絞り込まれないまま表示されて、カスタム投稿は表示されないようでした。
86
+ ```php
87
+ $args1 = array(
88
+ 'post_type' => 'post',
89
+ 'tax_query' => array(
90
+ 'relation' => 'OR',
91
+ array(
92
+ 'taxonomy' => 'category',
93
+ 'field' => 'slug',
94
+ 'terms' => array( 'cate1', 'cate2' ),
95
+ 'include_children' => true
96
+ )
97
+ )
98
+ );
99
+
100
+ $args2 = array(
101
+ 'post_type' => array('cutum1','cutum2','cutum3'),
102
+ );
103
+
104
+ $the_query = new WP_Query( $args1 || $args2 );
105
+ if ( $the_query->have_posts() ) :
106
+ while ( $the_query->have_posts() ) : $the_query->the_post();
107
+ //ループするテンプレート
108
+ endwhile;
109
+ endif;
110
+ wp_reset_postdata();
111
+
112
+ ```