Parcourir la source

:sparkles: added orderby and order

tags/0.9.0
J il y a 4 ans
Parent
révision
3347fb2a2d
1 fichiers modifiés avec 13 ajouts et 12 suppressions
  1. 13
    12
      plugins/cia-endpoints/includes/class.make-endpoint.php

+ 13
- 12
plugins/cia-endpoints/includes/class.make-endpoint.php Voir le fichier

@@ -46,17 +46,14 @@ class Make_Endpoint_For extends WP_REST_Controller {
46 46
         $args = array(
47 47
             'post_type' => $this->post_type,
48 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 49
             'posts_per_page' => -1,
51
-            'page' => 1
52
-            // Order DOES NOT WORK because you're
53
-            // returning the posts by id - DUH
54
-            // 'orderby' => 'date',
55
-            // 'order' => 'DESC'
50
+            'page' => 1,
51
+            'orderby' => 'date',
52
+            'order' => 'DESC'
56 53
         );
57 54
 
58
-        // Get parameters from request
59
-        // /<id>?limit=<num>
55
+        // Get parameters from request,
56
+        // /?limit=<num>&?orderby=<str>&?order=<str>
60 57
         $params = $request->get_params();
61 58
         if(intval($params['limit']) > 0) {
62 59
             $args['posts_per_page'] = intval($params['limit']);
@@ -68,8 +65,12 @@ class Make_Endpoint_For extends WP_REST_Controller {
68 65
             $args['page'] = intval($params['p']);
69 66
             $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
70 67
         }
71
-        // !: Add order asc/desc
72
-        // !: Add orderby
68
+        if($params['orderby']) {
69
+            $args['orderby'] = $params['orderby'];
70
+        }
71
+        if($params['order']) {
72
+            $args['order'] = $params['order'];
73
+        }
73 74
 
74 75
         return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
75 76
     }
@@ -115,7 +116,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
115 116
         foreach( $found_posts as $post ) {
116 117
             // print_r($args);
117 118
             // print_r($post);
118
-            $formatted = default_post_format( $post, true );
119
+            $formatted = default_post_format( $post, false );
119 120
 
120 121
             // For your hero URL
121 122
             $formatted[hero] = get_post_meta( $post->ID, 'hero_header', true );
@@ -133,7 +134,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
133 134
         }
134 135
         wp_reset_postdata();
135 136
         // Uncomment to include pagination and query information
136
-        // $collection['_meta'] = $args;
137
+        // $collection['_meta'] = $q;
137 138
         return $collection;
138 139
     }
139 140
 }

Chargement…
Annuler
Enregistrer