Преглед изворни кода

feat: added sort-by api end-points | added by-alpha

tags/0.9.0
J пре 6 година
родитељ
комит
d45cde55d3

+ 10
- 1
plugins/cia-endpoints/cia-end-points.php Прегледај датотеку

@@ -17,6 +17,7 @@
17 17
 if ( ! defined( 'WPINC' ) ) { die; }
18 18
 
19 19
 require_once('includes/class.make-endpoint.php');
20
+require_once('includes/class.make-sortby.php');
20 21
 
21 22
 /**
22 23
  * The standard wordpress post_types
@@ -44,4 +45,12 @@ add_action( 'rest_api_init', function () {
44 45
 add_action( 'rest_api_init', function () {
45 46
     $media_controller = new Make_Endpoint_For('artist');
46 47
     $media_controller->register_custom_route('artists');
47
-});
48
+});
49
+
50
+/**
51
+ * Craft in America custom sort_types
52
+ */
53
+add_action( 'rest_api_init', function () {
54
+    $media_controller = new Make_Sort_By('by_alpha', 'artist');
55
+    $media_controller->register_custom_route('sort/by-alpha');
56
+});

+ 53
- 0
plugins/cia-endpoints/includes/class.make-sortby.php Прегледај датотеку

@@ -0,0 +1,53 @@
1
+<?php
2
+class Make_Sort_By extends WP_REST_Controller {
3
+    private $post_type;
4
+    private $sort_type;
5
+    function __construct($sort_type, $post_type) {
6
+        $this->post_type = $post_type;
7
+        $this->sort_type = $sort_type;
8
+    }
9
+    /**
10
+     * Register the routes for the objects of the controller.
11
+     */
12
+    public function register_custom_route($route) {
13
+        $version = '2';
14
+        $namespace = 'craft/v' . $version;
15
+        register_rest_route( $namespace, '/' . $route . '/' . $this->post_type, [
16
+            array(
17
+                'methods'  => WP_REST_Server::READABLE,
18
+                'callback' => array( $this, $this->sort_type )
19
+            ),
20
+        ]);
21
+    }
22
+
23
+    public function by_alpha( $request ) {
24
+        $params = $request->get_params();
25
+        global $wpdb;
26
+        $res = $wpdb->get_results($wpdb->prepare(
27
+            "SELECT * FROM wp_posts
28
+            WHERE post_type = %s 
29
+            AND post_status='publish'
30
+            ORDER BY SUBSTRING_INDEX(post_title, ' ', -1)",
31
+            $this->post_type
32
+        ));
33
+        wp_reset_postdata();
34
+
35
+        $collection = array();
36
+    
37
+        forEach( $res as $key=>$item ) {
38
+            $filtered = array();
39
+            $filtered[id] = $item->ID;
40
+            $filtered[slug] = $item->post_name;
41
+            $filtered[type] = $item->post_type;
42
+            $filtered[title] = $item->post_title;
43
+            $filtered[excerpt] = $item->post_excerpt;
44
+            $filtered[date] = $item->post_date;
45
+            $filtered[content] = $item->post_content;
46
+            $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
47
+            $collection[$key] = $filtered;
48
+        };
49
+        return new WP_REST_Response( $collection, 200 );
50
+    }
51
+}
52
+
53
+?>

+ 1
- 1
plugins/cia-endpoints/includes/related-items.php Прегледај датотеку

@@ -55,7 +55,7 @@ function p2p_related_to($id, $type) {
55 55
         'post_type' => 'any',
56 56
         'include' => $related_posts_ids
57 57
     );
58
-    
58
+    wp_reset_postdata();
59 59
     return prepare_related_items($args);
60 60
 }
61 61
 

Loading…
Откажи
Сачувај