質問編集履歴

2

訂正

2016/10/06 06:53

投稿

kenny_sayama
kenny_sayama

スコア1036

test CHANGED
File without changes
test CHANGED
@@ -172,14 +172,10 @@
172
172
 
173
173
 
174
174
 
175
+ ```
175
176
 
176
177
 
177
178
 
179
+ 以上のレコードを取得してダンプしますと、
178
180
 
179
-
180
-
181
-
182
-
183
-
184
-
185
- ```
181
+ postsが1であるのに、カテゴリーが2つ存在するため、配列が2つに分かれてしまいます。

1

訂正

2016/10/06 06:53

投稿

kenny_sayama
kenny_sayama

スコア1036

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,159 @@
27
27
  $sql = "SELECT * FROM $wpdb->posts AS P JOIN $wpdb->term_relationships AS TR ON P.id = TR.object_id JOIN $wpdb->term_taxonomy AS TT ON TR.term_taxonomy_id = TT.term_taxonomy_id JOIN $wpdb->terms AS T ON TR.term_taxonomy_id = T.term_id WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 30";
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ ###データサンプル
34
+
35
+
36
+
37
+ 該当するテーブル
38
+
39
+
40
+
41
+ * wp_posts
42
+
43
+ * wp_terms
44
+
45
+ * wp_term_relationships
46
+
47
+ * wp_term_taxonomy
48
+
49
+
50
+
51
+
52
+
53
+ ######関係のありそうな部分のカラムだけ載せておきます。
54
+
55
+ wp_postsの構造(記事情報が格納されている)
56
+
57
+
58
+
59
+ * id
60
+
61
+ * title
62
+
63
+
64
+
65
+ wp_termの構造(カテゴリとタグが格納されている)
66
+
67
+
68
+
69
+ * term_id
70
+
71
+ * name (カテゴリ or タグの名前)
72
+
73
+ * slug (カテゴリ or タグのスラッグ名)
74
+
75
+
76
+
77
+ wp_term_relationshipsの構造()
78
+
79
+
80
+
81
+ * object_id
82
+
83
+ * term_taxonomy_id
84
+
85
+
86
+
87
+ wp_term_taxonomy
88
+
89
+
90
+
91
+ * term_taxonomy_id
92
+
93
+ * term_id
94
+
95
+ * taxonomy (category or post_tagが格納されこちらでタグかカテゴリの判別がされている)
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+ 例:
104
+
105
+ 以下のような1記事のレコードがあったとします。
106
+
107
+
108
+
109
+ ```
110
+
111
+ // wp_posts(記事データ)
112
+
113
+ * id = 1
114
+
115
+ * title = hoge
116
+
117
+
118
+
119
+
120
+
121
+ // wp_term_relationship(記事に関連付けを行う)
122
+
123
+ * object_id = 1(postの記事に紐づいてる)
124
+
125
+ * term_taxonomy_id = 1
126
+
127
+
128
+
129
+ * object_id = 1
130
+
131
+ * term_taxonomy_id = 2
132
+
133
+
134
+
135
+
136
+
137
+ // wp_terms(カテゴリーとタグが格納されている)
138
+
139
+ * term_id = 1
140
+
141
+ * name = 旅行
142
+
143
+ * slug = trip
144
+
145
+
146
+
147
+ * term_id = 2
148
+
149
+ * name = フード
150
+
151
+ * slug = food
152
+
153
+
154
+
155
+ // wp_term_taxonomy(カテゴリーかタグか判断させるテーブル)
156
+
157
+ * term_taxonomy_id = 1
158
+
159
+ * term_id = 1
160
+
161
+ * taxonomy = post_tag
162
+
163
+
164
+
165
+ * term_taxonomy_id = 2
166
+
167
+ * term_id = 2
168
+
169
+ * taxonomy = post_tag
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+ ```