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

回答編集履歴

1

反応無いから追記

2018/01/08 05:11

投稿

退会済みユーザー
answer CHANGED
@@ -1,2 +1,62 @@
1
- ```save_post```を使ったらどうでしょうか。
1
+ `save_post`を使ったらどうでしょうか。
2
- [https://codex.wordpress.org/Plugin_API/Action_Reference/save_post](https://codex.wordpress.org/Plugin_API/Action_Reference/save_post)
2
+ [https://codex.wordpress.org/Plugin_API/Action_Reference/save_post](https://codex.wordpress.org/Plugin_API/Action_Reference/save_post)
3
+
4
+ 追記
5
+
6
+ 無視?
7
+ 何か反応あってもよくない?
8
+
9
+ `get_field`やとDBに保存したものしか取れんのが原因。
10
+ だからこう。
11
+ ```
12
+ $cal_fields = get_field("switch", $post->ID);
13
+
14
+ $cal_fields = $_POST['fields']['フィールドキー'];
15
+ ```
16
+
17
+ これでもええんちゃうかと思うけど?
18
+ ```
19
+ function makes_json( $post_id, $post, $update ) {
20
+ if (function_exists('xdebug_disable')) {
21
+ xdebug_disable();
22
+ }
23
+ if( $post->post_status === 'publish' ) {
24
+ // Get ALL Posts
25
+ $args = array(
26
+ 'numberposts' => -1,
27
+ 'orderby' => 'post_date',
28
+ 'order' => 'DESC',
29
+ );
30
+ $posts = get_posts($args);
31
+ $homeUrl = 'http:test.jp/static_directory';
32
+ if (count($posts) == 0) { return; }
33
+ foreach ($posts as $post) {
34
+ $cal_fields = get_field("switch", $post->ID);
35
+ $output_json[date('Ym', strtotime($post->post_date))][] = array(
36
+ 'title' => $post->post_title,
37
+ 'start' => $post->post_date,
38
+ 'url' => $homeUrl . '/archives/' . $post->ID,
39
+ 'value' => $cal_fields,
40
+ );
41
+ }
42
+
43
+ // Get Upload Directory
44
+ $upload_dir = wp_upload_dir();
45
+ $upload_dir = $upload_dir['basedir'].'/json';
46
+
47
+ // Create Directory
48
+ if (file_exists($upload_dir) == FALSE) {
49
+ mkdir($upload_dir, 0777);
50
+ }
51
+
52
+ // Delete All JSON Files
53
+ array_map('unlink', glob($upload_dir.'/*.json'));
54
+
55
+ // Create JSON Files
56
+ foreach ($output_json as $key => $value) {
57
+ file_put_contents($upload_dir . '/'.$key.'.json', json_encode($value));
58
+ }
59
+ }
60
+ }
61
+ add_action('save_post', 'makes_json', 99, 3);
62
+ ```