Quellcode durchsuchen

:recycle: returning lists of posts with pagination

tags/0.9.0
J vor 4 Jahren
Ursprung
Commit
78ee2ed316
1 geänderte Dateien mit 34 neuen und 20 gelöschten Zeilen
  1. 34
    20
      plugins/cia-endpoints/includes/class.make-endpoint.php

+ 34
- 20
plugins/cia-endpoints/includes/class.make-endpoint.php Datei anzeigen

44
     }
44
     }
45
     public function get_all_items( $request ) {
45
     public function get_all_items( $request ) {
46
         $args = array(
46
         $args = array(
47
-            'numberposts' => 999999,
48
             'post_type' => $this->post_type,
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
             // Order DOES NOT WORK because you're
52
             // Order DOES NOT WORK because you're
50
             // returning the posts by id - DUH
53
             // returning the posts by id - DUH
51
             // 'orderby' => 'date',
54
             // 'orderby' => 'date',
55
         // Get parameters from request
58
         // Get parameters from request
56
         // /<id>?limit=<num>
59
         // /<id>?limit=<num>
57
         $params = $request->get_params();
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
         // !: Add order asc/desc
71
         // !: Add order asc/desc
61
         // !: Add orderby
72
         // !: Add orderby
62
 
73
 
86
             
97
             
87
             if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
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
         wp_reset_postdata();
102
         wp_reset_postdata();
92
 
103
 
93
         return $collection;
104
         return $collection;
94
     }
105
     }
95
     public function prepare_all_items_for_response( $args ) {
106
     public function prepare_all_items_for_response( $args ) {
96
-        $collection = array();
107
+        $collection = [];
97
         $alphabet_section = 'a';
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
             // print_r($args);
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
             // For your hero URL
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
             // Sticky
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
         wp_reset_postdata();
134
         wp_reset_postdata();
122
-
135
+        
136
+        $collection['_meta'] = $args;
123
         return $collection;
137
         return $collection;
124
     }
138
     }
125
 }
139
 }

Laden…
Abbrechen
Speichern