質問編集履歴

5

xx

2023/02/27 22:09

投稿

erp
erp

スコア46

test CHANGED
File without changes
test CHANGED
@@ -133,8 +133,9 @@
133
133
  ```php
134
134
  <?php
135
135
 
136
- function autoUpload($post_id, $post) {
136
+ function autoUpload($post) {
137
+ global $post;
137
- $imgID = $post_id;
138
+ $imgID = $post->ID;
138
139
  $imgAlt = get_post_meta($imgID, '_wp_attachment_image_alt', true);
139
140
  $imgTitle = $post->post_title;
140
141
  $imgCaption = $post->post_excerpt;
@@ -147,9 +148,7 @@
147
148
  // ここで、条件[jpg]とか
148
149
  if ($mime == "image/jpeg" || $mime == "image/png" || $mime == "image/gif") {
149
150
 
150
- $post_types = array(
151
- 'works', 'journal',
151
+ $post_types = array('works', 'journal');
152
- );
153
152
  foreach ($post_types as $post_type) :
154
153
  if ($post_type == 'works') {
155
154
  $category = 'gallery';
@@ -169,7 +168,17 @@
169
168
  $data = array(
170
169
  'ID' => $ID,
171
170
  'post_type' => $post_type,
171
+ 'tax_input' => [
172
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
172
+ $post_type . '-category' => [
173
+ $category,
174
+ ],
175
+ $post_type . '-tag' => [
176
+ '',
177
+ ]
178
+ ],
179
+ 'meta_input' => [
180
+ 'custom_caption' => $imgDescription,
181
+ ],
173
182
  'post_status' => 'publish',
174
183
  'post_title' => $post_title,
175
184
  'post_author' => $imgAuthor,
@@ -199,14 +208,24 @@
199
208
  $data = array(
200
209
  'ID' => $ID,
201
210
  'post_type' => $post_type,
211
+ 'tax_input' => [
202
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
212
+ $post_type . '-category' => [
213
+ $category,
214
+ ],
215
+ $post_type . '-tag' => [
216
+ '',
217
+ ]
218
+ ],
219
+ 'meta_input' => [
220
+ 'custom_caption' => $imgDescription,
221
+ ],
203
222
  'post_status' => 'publish',
204
223
  'post_title' => $imgAlt,
205
224
  'post_author' => $imgAuthor,
206
225
  'post_content' => '
207
226
  <video controls>
208
227
  <source src="' . $imgUrl . '" type="' . $mime . '">
209
- <p>このブラウザーは HTML5 video に対応していません。/p>
228
+ <p>このブラウザーは HTML5 video に対応していません。</p>
210
229
  </video>
211
230
  ',
212
231
  'post_date' => $imgDate,
@@ -215,13 +234,9 @@
215
234
  'page_template' => 'page-portfolio.php',
216
235
  );
217
236
  }
218
-
219
- $newPost_id = wp_insert_post($data);
237
+ wp_insert_post($data);
220
- if ($newPost_id) {
221
- update_post_meta($newPost_id, 'custom_caption', $imgDescription);
222
- }
223
238
  }
224
- add_action('inherit_attachment', 'autoUpload', 10);
239
+ add_action('save_post_attachment', 'autoUpload', 10);
225
240
 
226
241
  ```
227
242
  これでも動きません。

4

xx

2023/02/27 20:33

投稿

erp
erp

スコア46

test CHANGED
File without changes
test CHANGED
@@ -133,9 +133,8 @@
133
133
  ```php
134
134
  <?php
135
135
 
136
- function autoUpload($post) {
136
+ function autoUpload($post_id, $post) {
137
- global $post;
138
- $imgID = $post->ID;
137
+ $imgID = $post_id;
139
138
  $imgAlt = get_post_meta($imgID, '_wp_attachment_image_alt', true);
140
139
  $imgTitle = $post->post_title;
141
140
  $imgCaption = $post->post_excerpt;
@@ -154,8 +153,10 @@
154
153
  foreach ($post_types as $post_type) :
155
154
  if ($post_type == 'works') {
156
155
  $category = 'gallery';
156
+ $post_title = $imgAlt;
157
157
  } else {
158
158
  $category = 'update';
159
+ $post_title = '「' . $imgAlt . '」更新';
159
160
  }
160
161
  $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
161
162
  $response = @file_get_contents($url);
@@ -165,12 +166,12 @@
165
166
  $ID = '';
166
167
  }
167
168
 
168
- $date = array(
169
+ $data = array(
169
170
  'ID' => $ID,
170
171
  'post_type' => $post_type,
171
172
  'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
172
173
  'post_status' => 'publish',
173
- 'post_title' => $imgAlt,
174
+ 'post_title' => $post_title,
174
175
  'post_author' => $imgAuthor,
175
176
  'post_content' => '<img src="' . $imgUrl . '" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
176
177
  'post_date' => $imgDate,
@@ -195,7 +196,7 @@
195
196
  $ID = '';
196
197
  }
197
198
 
198
- $date = array(
199
+ $data = array(
199
200
  'ID' => $ID,
200
201
  'post_type' => $post_type,
201
202
  'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
@@ -215,12 +216,12 @@
215
216
  );
216
217
  }
217
218
 
218
- $newPost_id = wp_insert_post($date);
219
+ $newPost_id = wp_insert_post($data);
219
220
  if ($newPost_id) {
220
221
  update_post_meta($newPost_id, 'custom_caption', $imgDescription);
221
222
  }
222
223
  }
223
- add_action('publish_attachment', 'autoUpload');
224
+ add_action('inherit_attachment', 'autoUpload', 10);
224
225
 
225
226
  ```
226
227
  これでも動きません。

3

xx

2023/02/27 19:35

投稿

erp
erp

スコア46

test CHANGED
File without changes
test CHANGED
@@ -145,67 +145,60 @@
145
145
  $imgAuthor = $post->post_author;
146
146
  $imgUrl = $post->guid;
147
147
 
148
- $post_type = '';
149
- $category = '';
150
-
151
- $terms = get_the_terms($imgID, $post_type . '-tag');
152
- if ($terms && !is_wp_error($terms)) {
153
- foreach ($terms as $term) {
154
- $tag = $term->slug;
155
- }
156
- }
157
- $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
158
- $response = @file_get_contents($url);
159
- if ($response !== false) {
160
- $ID = $post->url_to_postid($url);
161
- } else {
162
- $ID = '';
163
- }
164
-
165
148
  // ここで、条件[jpg]とか
166
149
  if ($mime == "image/jpeg" || $mime == "image/png" || $mime == "image/gif") {
167
150
 
168
151
  $post_types = array(
169
152
  'works', 'journal',
170
153
  );
171
- if ($post_types && !is_wp_error($post_types)) :
172
- foreach ($post_types as $post_type) :
154
+ foreach ($post_types as $post_type) :
173
- if ($post_type == 'works') {
155
+ if ($post_type == 'works') {
174
- $category == 'gallery';
156
+ $category = 'gallery';
175
- } else {
157
+ } else {
176
- $category == 'update';
158
+ $category = 'update';
177
- }
159
+ }
178
-
160
+ $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
161
+ $response = @file_get_contents($url);
162
+ if ($response !== false) {
163
+ $ID = $post->url_to_postid($url);
164
+ } else {
165
+ $ID = '';
166
+ }
167
+
179
- $date = array(
168
+ $date = array(
180
- 'ID' => $ID,
169
+ 'ID' => $ID,
181
- 'post_type' => $post_type,
170
+ 'post_type' => $post_type,
182
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
171
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
183
- 'post_status' => 'publish',
172
+ 'post_status' => 'publish',
184
- 'post_title' => $imgAlt,
173
+ 'post_title' => $imgAlt,
185
- 'post_author' => $imgAuthor,
174
+ 'post_author' => $imgAuthor,
186
- 'post_content' => '<img src="' . $imgUrl . '" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
175
+ 'post_content' => '<img src="' . $imgUrl . '" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
187
- 'post_date' => $imgDate,
176
+ 'post_date' => $imgDate,
188
- 'post_name' => $imgTitle,
177
+ 'post_name' => $imgTitle,
189
- 'post_excerpt' => $imgCaption,
178
+ 'post_excerpt' => $imgCaption,
190
- 'page_template' => 'page-portfolio.php',
179
+ 'page_template' => 'page-portfolio.php',
191
- );
180
+ );
192
- endforeach;
181
+ endforeach;
193
- endif;
194
182
  } elseif ($mime == 'video/mp4') {
195
183
 
196
- $post_types = array(
197
- 'works',
184
+ $post_type = 'works';
198
- );
199
185
  if ($post_type == 'works') {
200
- $category == 'videos';
186
+ $category = 'videos';
201
187
  } else {
202
- $category == '';
188
+ $category = '';
189
+ }
190
+ $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
191
+ $response = @file_get_contents($url);
192
+ if ($response !== false) {
193
+ $ID = $post->url_to_postid($url);
194
+ } else {
195
+ $ID = '';
203
196
  }
204
197
 
205
198
  $date = array(
206
199
  'ID' => $ID,
207
200
  'post_type' => $post_type,
208
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
201
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => ''),
209
202
  'post_status' => 'publish',
210
203
  'post_title' => $imgAlt,
211
204
  'post_author' => $imgAuthor,
@@ -227,7 +220,7 @@
227
220
  update_post_meta($newPost_id, 'custom_caption', $imgDescription);
228
221
  }
229
222
  }
230
- add_action('publish_attachment', 'autoUpload', 9);
223
+ add_action('publish_attachment', 'autoUpload');
231
224
 
232
225
  ```
233
226
  これでも動きません。

2

更新

2023/02/27 19:11

投稿

erp
erp

スコア46

test CHANGED
File without changes
test CHANGED
@@ -133,105 +133,101 @@
133
133
  ```php
134
134
  <?php
135
135
 
136
- new autoUpload;
137
-
138
- class autoUpload {
139
- function __construct() {
136
+ function autoUpload($post) {
137
+ global $post;
138
+ $imgID = $post->ID;
140
- add_action('save_post_attachment', 'autoUpload', 9);
139
+ $imgAlt = get_post_meta($imgID, '_wp_attachment_image_alt', true);
140
+ $imgTitle = $post->post_title;
141
+ $imgCaption = $post->post_excerpt;
142
+ $imgDescription = $post->post_content;
143
+ $imgDate = $post->post_date;
144
+ $mime = $post->post_mime_type;
145
+ $imgAuthor = $post->post_author;
146
+ $imgUrl = $post->guid;
147
+
148
+ $post_type = '';
149
+ $category = '';
150
+
151
+ $terms = get_the_terms($imgID, $post_type . '-tag');
152
+ if ($terms && !is_wp_error($terms)) {
153
+ foreach ($terms as $term) {
154
+ $tag = $term->slug;
141
- }
155
+ }
142
-
143
- function autoUpload($post_id, $post) {
144
- $imgID = $post_id;
145
- $imgAlt = get_post_meta($imgID, '_wp_attachment_image_alt', true);
146
- $imgTitle = $post->title;
147
- $imgCaption = $post->post_excerpt;
148
- $imgDescription = $post->post_content;
149
- $imgDate = $post->post_date;
150
- $mime = get_post_mime_type($imgID);
151
-
152
- $post_type = '';
153
- $category = '';
154
-
155
- $terms = get_the_terms($imgID, $post_type . '-tag');
156
- if ($terms && !is_wp_error($terms)) {
157
- foreach ($terms as $term) {
158
- $tag = $term->slug;
159
- }
156
+ }
157
+ $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
158
+ $response = @file_get_contents($url);
159
+ if ($response !== false) {
160
+ $ID = $post->url_to_postid($url);
161
+ } else {
162
+ $ID = '';
160
- }
163
+ }
164
+
165
+ // ここで、条件[jpg]とか
166
+ if ($mime == "image/jpeg" || $mime == "image/png" || $mime == "image/gif") {
167
+
168
+ $post_types = array(
169
+ 'works', 'journal',
170
+ );
171
+ if ($post_types && !is_wp_error($post_types)) :
172
+ foreach ($post_types as $post_type) :
173
+ if ($post_type == 'works') {
174
+ $category == 'gallery';
175
+ } else {
176
+ $category == 'update';
177
+ }
178
+
179
+ $date = array(
180
+ 'ID' => $ID,
181
+ 'post_type' => $post_type,
161
- $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
182
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
183
+ 'post_status' => 'publish',
162
- $response = @file_get_contents($url);
184
+ 'post_title' => $imgAlt,
185
+ 'post_author' => $imgAuthor,
186
+ 'post_content' => '<img src="' . $imgUrl . '" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
187
+ 'post_date' => $imgDate,
188
+ 'post_name' => $imgTitle,
189
+ 'post_excerpt' => $imgCaption,
190
+ 'page_template' => 'page-portfolio.php',
191
+ );
192
+ endforeach;
193
+ endif;
194
+ } elseif ($mime == 'video/mp4') {
195
+
196
+ $post_types = array(
197
+ 'works',
198
+ );
163
- if ($response !== false) {
199
+ if ($post_type == 'works') {
164
- $ID = url_to_postid($url);
200
+ $category == 'videos';
165
201
  } else {
166
- $ID = '';
202
+ $category == '';
167
- }
203
+ }
168
-
169
- // ここで、条件[jpg]とか
204
+
170
- if ($mime == "image/jpeg" || $mime == "image/png" || $mime == "image/gif") {
171
-
172
- $post_types = array(
173
- 'works', 'journal',
174
- );
175
- if ($post_types && !is_wp_error($post_types)) :
176
- foreach ($post_types as $post_type) :
177
- if ($post_type == 'works') {
178
- $category == 'gallery';
179
- } else {
180
- $category == 'update';
181
- }
182
- endforeach;
183
- endif;
184
-
185
- $date = array(
205
+ $date = array(
186
- 'ID' => $post->$ID,
206
+ 'ID' => $ID,
187
- 'post_type' => $post_type,
207
+ 'post_type' => $post_type,
188
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
208
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
189
- 'post_status' => 'publish',
209
+ 'post_status' => 'publish',
190
- 'post_title' => $imgAlt,
210
+ 'post_title' => $imgAlt,
191
- 'post_author' => 1,
211
+ 'post_author' => $imgAuthor,
192
- 'post_content' => '<img src="' . home_url() . '/wp-content/uploads/img/' . get_the_time('Y/m') . '/' . $imgTitle . '.jpg" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
193
- 'post_date' => $imgDate,
194
- 'post_name' => $imgTitle,
195
- 'post_excerpt' => $imgCaption,
196
- 'page_template' => 'page-portfolio.php',
197
- );
198
- } elseif ($mime == 'video/mp4') {
199
-
200
- $post_types = array(
201
- 'works',
202
- );
203
- if ($post_type == 'works') {
204
- $category == 'videos';
205
- } else {
206
- $category == '';
207
- }
208
-
209
- $date = array(
210
- 'ID' => $post->$ID,
211
- 'post_type' => $post_type,
212
- 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
213
- 'post_status' => 'publish',
214
- 'post_title' => $imgAlt,
215
- 'post_author' => 1,
216
- 'post_content' => '
212
+ 'post_content' => '
217
213
  <video controls>
218
- <source src="' . home_url() . '/wp-content/uploads/video/' . get_the_time('Y/m') . '/' . $imgTitle . '.mp4" type="video/mp4">
214
+ <source src="' . $imgUrl . '" type="' . $mime . '">
219
215
  <p>このブラウザーは HTML5 video に対応していません。/p>
220
216
  </video>
221
217
  ',
222
- 'post_date' => $imgDate,
218
+ 'post_date' => $imgDate,
223
- 'post_name' => $imgTitle,
219
+ 'post_name' => $imgTitle,
224
- 'post_excerpt' => $imgCaption,
220
+ 'post_excerpt' => $imgCaption,
225
- 'page_template' => 'page-portfolio.php',
221
+ 'page_template' => 'page-portfolio.php',
226
- );
222
+ );
227
- }
223
+ }
228
-
224
+
229
- $newPost_id = wp_insert_post($date);
225
+ $newPost_id = wp_insert_post($date);
230
- if ($newPost_id) {
226
+ if ($newPost_id) {
231
- update_post_meta($newPost_id, 'custom_caption', $imgDescription);
227
+ update_post_meta($newPost_id, 'custom_caption', $imgDescription);
232
- }
233
228
  }
234
229
  }
230
+ add_action('publish_attachment', 'autoUpload', 9);
235
231
 
236
232
  ```
237
233
  これでも動きません。

1

更新

2023/02/27 18:30

投稿

erp
erp

スコア46

test CHANGED
File without changes
test CHANGED
@@ -130,6 +130,112 @@
130
130
  ```
131
131
 
132
132
  ### 試したこと
133
+ ```php
134
+ <?php
135
+
136
+ new autoUpload;
137
+
138
+ class autoUpload {
139
+ function __construct() {
140
+ add_action('save_post_attachment', 'autoUpload', 9);
141
+ }
142
+
143
+ function autoUpload($post_id, $post) {
144
+ $imgID = $post_id;
145
+ $imgAlt = get_post_meta($imgID, '_wp_attachment_image_alt', true);
146
+ $imgTitle = $post->title;
147
+ $imgCaption = $post->post_excerpt;
148
+ $imgDescription = $post->post_content;
149
+ $imgDate = $post->post_date;
150
+ $mime = get_post_mime_type($imgID);
151
+
152
+ $post_type = '';
153
+ $category = '';
154
+
155
+ $terms = get_the_terms($imgID, $post_type . '-tag');
156
+ if ($terms && !is_wp_error($terms)) {
157
+ foreach ($terms as $term) {
158
+ $tag = $term->slug;
159
+ }
160
+ }
161
+ $url = home_url() . '/' . $post_type . '/' . $category . '/' . $imgTitle . '/';
162
+ $response = @file_get_contents($url);
163
+ if ($response !== false) {
164
+ $ID = url_to_postid($url);
165
+ } else {
166
+ $ID = '';
167
+ }
168
+
169
+ // ここで、条件[jpg]とか
170
+ if ($mime == "image/jpeg" || $mime == "image/png" || $mime == "image/gif") {
171
+
172
+ $post_types = array(
173
+ 'works', 'journal',
174
+ );
175
+ if ($post_types && !is_wp_error($post_types)) :
176
+ foreach ($post_types as $post_type) :
177
+ if ($post_type == 'works') {
178
+ $category == 'gallery';
179
+ } else {
180
+ $category == 'update';
181
+ }
182
+ endforeach;
183
+ endif;
184
+
185
+ $date = array(
186
+ 'ID' => $post->$ID,
187
+ 'post_type' => $post_type,
188
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
189
+ 'post_status' => 'publish',
190
+ 'post_title' => $imgAlt,
191
+ 'post_author' => 1,
192
+ 'post_content' => '<img src="' . home_url() . '/wp-content/uploads/img/' . get_the_time('Y/m') . '/' . $imgTitle . '.jpg" alt="' . $imgAlt . '" data-caption="' . $imgAlt . '">',
193
+ 'post_date' => $imgDate,
194
+ 'post_name' => $imgTitle,
195
+ 'post_excerpt' => $imgCaption,
196
+ 'page_template' => 'page-portfolio.php',
197
+ );
198
+ } elseif ($mime == 'video/mp4') {
199
+
200
+ $post_types = array(
201
+ 'works',
202
+ );
203
+ if ($post_type == 'works') {
204
+ $category == 'videos';
205
+ } else {
206
+ $category == '';
207
+ }
208
+
209
+ $date = array(
210
+ 'ID' => $post->$ID,
211
+ 'post_type' => $post_type,
212
+ 'tax_input' => array($post_type . '-category' => $category, $post_type . '-tag' => $tag),
213
+ 'post_status' => 'publish',
214
+ 'post_title' => $imgAlt,
215
+ 'post_author' => 1,
216
+ 'post_content' => '
217
+ <video controls>
218
+ <source src="' . home_url() . '/wp-content/uploads/video/' . get_the_time('Y/m') . '/' . $imgTitle . '.mp4" type="video/mp4">
219
+ <p>このブラウザーは HTML5 video に対応していません。/p>
220
+ </video>
221
+ ',
222
+ 'post_date' => $imgDate,
223
+ 'post_name' => $imgTitle,
224
+ 'post_excerpt' => $imgCaption,
225
+ 'page_template' => 'page-portfolio.php',
226
+ );
227
+ }
228
+
229
+ $newPost_id = wp_insert_post($date);
230
+ if ($newPost_id) {
231
+ update_post_meta($newPost_id, 'custom_caption', $imgDescription);
232
+ }
233
+ }
234
+ }
235
+
236
+ ```
237
+ これでも動きません。
238
+
133
239
  色々調べましたが、なぜ動かないのか分からずお手上げです。
134
240
  $imgxx が取得できていないのでしょうか…
135
241
  お助けいただけると、大変助かります。