質問編集履歴
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,55 +13,55 @@
|
|
13
13
|
独自エンドポイントの設定↓
|
14
14
|
```php
|
15
15
|
<?php
|
16
|
-
function
|
16
|
+
function get_work_fields($work) {
|
17
|
-
$
|
17
|
+
$work_custom = get_post_custom($work->ID);
|
18
|
-
// var_dump($
|
18
|
+
// var_dump($work);
|
19
|
-
$
|
19
|
+
$work_fields = [
|
20
|
-
'id' => $
|
20
|
+
'id' => $work->ID,
|
21
21
|
//公開日
|
22
|
-
'time' => date('Y.m.d', strtotime($
|
22
|
+
'time' => date('Y.m.d', strtotime($works->post_date)),
|
23
|
-
'title' => $
|
23
|
+
'title' => $work->post_title,
|
24
24
|
];
|
25
25
|
|
26
|
-
return $
|
26
|
+
return $work_fields;
|
27
27
|
}
|
28
28
|
|
29
|
-
function
|
29
|
+
function get_works( $data ) {
|
30
|
-
if( $data['id']) return
|
30
|
+
if( $data['id']) return get_work_by_id($data);
|
31
31
|
|
32
32
|
$default_args = [
|
33
|
-
'post_type' => '
|
33
|
+
'post_type' => 'works',
|
34
34
|
'posts_per_page' => -1,
|
35
35
|
'post_status' => 'publish',
|
36
36
|
'orderby' => 'date',
|
37
37
|
'order' => 'DESC'
|
38
38
|
];
|
39
|
-
// $
|
39
|
+
// $works = new WP_Query( $default_args );
|
40
|
-
$
|
40
|
+
$works = get_posts($default_args);
|
41
|
-
// var_dump($
|
41
|
+
// var_dump($works);
|
42
|
-
if ( empty( $
|
42
|
+
if ( empty( $works ) ) {
|
43
43
|
return null;
|
44
44
|
}
|
45
45
|
|
46
|
-
$return_posts = array_map('
|
46
|
+
$return_posts = array_map('get_work_fields', $works);
|
47
47
|
|
48
48
|
return $return_posts;
|
49
49
|
}
|
50
50
|
|
51
|
-
function
|
51
|
+
function get_work_by_id( $data ) {
|
52
52
|
global $default_args;
|
53
53
|
$post = get_post( $data['id'], OBJECT, $default_args);
|
54
54
|
if ( empty( $post ) ) {
|
55
55
|
return null;
|
56
56
|
}
|
57
|
-
$return_posts =
|
57
|
+
$return_posts = get_work_fields($post);
|
58
58
|
return $return_posts;
|
59
59
|
}
|
60
60
|
|
61
61
|
add_action( 'rest_api_init', function () {
|
62
|
-
register_rest_route( 'custom/v1', '/
|
62
|
+
register_rest_route( 'custom/v1', '/works', array(
|
63
63
|
'methods' => 'GET',
|
64
|
-
'callback' => '
|
64
|
+
'callback' => 'get_works',
|
65
65
|
) );
|
66
66
|
});
|
67
67
|
```
|
1
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
独自エンドポイントの場合はクエリーパラメータでの操作は難しいのでしょうか?
|
5
5
|
ちなみにデータ自体はちゃんと取れています。
|
6
6
|
|
7
|
-
↓
|
7
|
+
↓下記のような感じでパラメータを使用して、記事の取得数などをいじりたいです
|
8
8
|
https://exapmle.com/wp-json/wp/v2/posts?page=[NUMBER]
|
9
9
|
|
10
10
|
クエリーパラメター参考記事
|