|
|
@@ -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
|
+?>
|