Просмотр исходного кода

:recycle: returning lists of posts with pagination

tags/0.9.0
J 4 лет назад
Родитель
Сommit
78ee2ed316
1 измененных файлов: 34 добавлений и 20 удалений
  1. 34
    20
      plugins/cia-endpoints/includes/class.make-endpoint.php

+ 34
- 20
plugins/cia-endpoints/includes/class.make-endpoint.php Просмотреть файл

@@ -44,8 +44,11 @@ class Make_Endpoint_For extends WP_REST_Controller {
44 44
     }
45 45
     public function get_all_items( $request ) {
46 46
         $args = array(
47
-            'numberposts' => 999999,
48 47
             'post_type' => $this->post_type,
48
+            'post_status' => ['publish'],
49
+            //(int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page' => -1 to show all posts. 
50
+            'posts_per_page' => -1,
51
+            'page' => 1
49 52
             // Order DOES NOT WORK because you're
50 53
             // returning the posts by id - DUH
51 54
             // 'orderby' => 'date',
@@ -55,8 +58,16 @@ class Make_Endpoint_For extends WP_REST_Controller {
55 58
         // Get parameters from request
56 59
         // /<id>?limit=<num>
57 60
         $params = $request->get_params();
58
-        if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
59
-        if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
61
+        if(intval($params['limit']) > 0) {
62
+            $args['posts_per_page'] = intval($params['limit']);
63
+        }
64
+        if(intval($params['id']) > 0) {
65
+            $args['include'] = array($params['id']);
66
+        }
67
+        if(intval($params['p']) > 1) {
68
+            $args['page'] = intval($params['p']);
69
+            $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
70
+        }
60 71
         // !: Add order asc/desc
61 72
         // !: Add orderby
62 73
 
@@ -86,40 +97,43 @@ class Make_Endpoint_For extends WP_REST_Controller {
86 97
             
87 98
             if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
88 99
 
89
-            $collection[$item->ID] = $filtered;
100
+            array_push($collection, $filtered);
90 101
         }
91 102
         wp_reset_postdata();
92 103
 
93 104
         return $collection;
94 105
     }
95 106
     public function prepare_all_items_for_response( $args ) {
96
-        $collection = array();
107
+        $collection = [];
97 108
         $alphabet_section = 'a';
109
+        
110
+        $q = new WP_Query( $args );
98 111
 
99
-        // https://developer.wordpress.org/reference/functions/get_posts/
100
-        foreach( get_posts($args) as $item ) {
112
+        $args['max'] = $q->max_num_pages;
113
+        $found_posts = $q->get_posts();
114
+
115
+        foreach( $found_posts as $post ) {
101 116
             // print_r($args);
102
-            // print_r($item);
103
-            $filtered = default_post_format( $item, true );
117
+            // print_r($post);
118
+            $formatted = default_post_format( $post, true );
104 119
 
105 120
             // For your hero URL
106
-            $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
121
+            $formatted[hero] = get_post_meta( $post->ID, 'hero_header', true );
107 122
 
108 123
             // Sticky
109
-            $filtered[sticky] = get_post_meta( $item->ID, 'is_sticky', true );
110
-
111
-            if($item->post_type == 'page') {
112
-                $filtered[content] = $item->post_content;
113
-            }
124
+            $formatted[sticky] = get_post_meta( $post->ID, 'is_sticky', true );
114 125
             
115
-            if($item->post_type == 'artist') {
116
-                // This is where you add to your letter counter
117
-            }
126
+            // Don't use the BLOCK system for pages for some reason
127
+            if($post->post_type == 'page') $formatted[content] = $post->post_content;
128
+            
129
+            // This is where you add to your letter counter
130
+            if($post->post_type == 'artist') {}
118 131
 
119
-            array_push($collection, $filtered);
132
+            array_push($collection, $formatted);
120 133
         }
121 134
         wp_reset_postdata();
122
-
135
+        
136
+        $collection['_meta'] = $args;
123 137
         return $collection;
124 138
     }
125 139
 }

Загрузка…
Отмена
Сохранить