Browse Source

:sparkles: sorting by current, upcoming, and past for events and exhibitions added to api

tags/0.9.0
j 4 years ago
parent
commit
9beb61142e

+ 11
- 1
plugins/cia-endpoints/cia-end-points.php View File

58
     $sort_controller->register_custom_route('artists/by-alpha');
58
     $sort_controller->register_custom_route('artists/by-alpha');
59
     $sort_controller = new Make_Sort_By('artist', 'by_material');
59
     $sort_controller = new Make_Sort_By('artist', 'by_material');
60
     $sort_controller->register_custom_route('artists/by-material');
60
     $sort_controller->register_custom_route('artists/by-material');
61
-    $sort_controller = new Make_Sort_By('event', 'by_upcoming_event');
61
+    $sort_controller = new Make_Sort_By('event', 'by_past');
62
+    $sort_controller->register_custom_route('events/by-past');
63
+    $sort_controller = new Make_Sort_By('exhibition', 'by_past');
64
+    $sort_controller->register_custom_route('exhibitions/by-past');
65
+    $sort_controller = new Make_Sort_By('event', 'by_current');
66
+    $sort_controller->register_custom_route('events/by-current');
67
+    $sort_controller = new Make_Sort_By('exhibition', 'by_current');
68
+    $sort_controller->register_custom_route('exhibitions/by-current');
69
+    $sort_controller = new Make_Sort_By('event', 'by_upcoming');
62
     $sort_controller->register_custom_route('events/by-upcoming');
70
     $sort_controller->register_custom_route('events/by-upcoming');
71
+    $sort_controller = new Make_Sort_By('exhibition', 'by_upcoming');
72
+    $sort_controller->register_custom_route('exhibitions/by-upcoming');
63
 });
73
 });
64
 
74
 
65
 /**
75
 /**

+ 47
- 20
plugins/cia-endpoints/includes/class.make-sortby.php View File

42
         $res = $wpdb->get_results($wpdb->prepare(
42
         $res = $wpdb->get_results($wpdb->prepare(
43
             "SELECT * FROM wp_posts
43
             "SELECT * FROM wp_posts
44
             WHERE post_type = %s
44
             WHERE post_type = %s
45
-            AND post_status='publish'",
45
+            AND post_status = 'publish'",
46
             $this->post_type
46
             $this->post_type
47
         ));
47
         ));
48
         wp_reset_postdata();
48
         wp_reset_postdata();
50
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
50
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
51
     }
51
     }
52
     
52
     
53
-    public function by_upcoming_event( $request   ) {
53
+    public function by_past( $request ) {
54
         global $wpdb;
54
         global $wpdb;
55
-        $time = new DateTime();
56
-        $timestamp = $time->getTimestamp();
55
+        $time = strval(time());
56
+        $end = 'exhibit-end-date';
57
+        if($this->post_type == 'event') {
58
+            $end = 'event-end-time';
59
+        }
57
         $res = $wpdb->get_results($wpdb->prepare(
60
         $res = $wpdb->get_results($wpdb->prepare(
58
-            "SELECT DISTINCT wp_posts.*
59
-            FROM wp_posts LEFT JOIN wp_postmeta AS ends
60
-            ON (ends.post_id = wp_posts.ID AND ends.meta_key='event-end-time' AND ends.meta_value <= %d)
61
-            WHERE post_type = %s
62
-            AND CONVERT('event-end-time', DATETIME) >= CONVERT('2021-10-31', DATETIME)
63
-            AND post_status='publish'
61
+            "SELECT DISTINCT wp_posts.* FROM wp_posts
62
+            JOIN wp_postmeta AS ends
63
+            ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value <= %s)
64
+            WHERE post_type = %s AND post_status = 'publish'
64
             ORDER BY ends.meta_value DESC",
65
             ORDER BY ends.meta_value DESC",
65
-            $time, $this->post_type
66
+            $end, $time, $this->post_type
66
         ));
67
         ));
67
         wp_reset_postdata();
68
         wp_reset_postdata();
69
+        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
70
+    }
68
 
71
 
72
+    public function by_current( $request ) {
73
+        global $wpdb;
74
+        $time = strval(time());
75
+        $start = 'exhibit-start-date';
76
+        $end = 'exhibit-end-date';
77
+        if($this->post_type == 'event') {
78
+            $start = 'event-start-time';
79
+            $end = 'event-end-time';
80
+        }
81
+        $res = $wpdb->get_results($wpdb->prepare(
82
+            "SELECT DISTINCT wp_posts.* FROM wp_posts 
83
+            JOIN wp_postmeta AS starts
84
+            ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s)
85
+            JOIN wp_postmeta AS ends
86
+            ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
87
+            WHERE post_type = %s AND post_status = 'publish'
88
+            ORDER BY starts.meta_value DESC",
89
+            $start, $time, $end, $time, $this->post_type
90
+        ));
91
+        wp_reset_postdata();
69
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
92
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
70
     }
93
     }
71
-    public function by_upcoming_exhibition( $request ) {
94
+    
95
+    public function by_upcoming( $request ) {
72
         global $wpdb;
96
         global $wpdb;
97
+        $time = strval(time());
98
+        $start = 'exhibit-start-date';
99
+        if($this->post_type == 'event') {
100
+            $start = 'event-start-time';
101
+        }
73
         $res = $wpdb->get_results($wpdb->prepare(
102
         $res = $wpdb->get_results($wpdb->prepare(
74
-            "SELECT DISTINCT wp_posts.*
75
-            FROM wp_posts LEFT JOIN wp_postmeta
76
-            ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='exhibit-end-date')
77
-            WHERE post_type = 'exhibition'
78
-            AND post_status='publish'
79
-            ORDER BY 'exhibit-end-date'",
80
-            $this->post_type
103
+            "SELECT DISTINCT wp_posts.* FROM wp_posts 
104
+            JOIN wp_postmeta AS starts
105
+            ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value >= %s)
106
+            WHERE post_type = %s AND post_status = 'publish'
107
+            ORDER BY starts.meta_value DESC",
108
+            $start, $time, $this->post_type
81
         ));
109
         ));
82
         wp_reset_postdata();
110
         wp_reset_postdata();
83
-
84
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
111
         return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
85
     }
112
     }
86
 
113
 

+ 2
- 0
plugins/cia-endpoints/includes/formats.php View File

43
     if($item->post_type === 'event') {
43
     if($item->post_type === 'event') {
44
         $filtered[start] = get_post_meta( $item->ID, 'event-start-time', true );
44
         $filtered[start] = get_post_meta( $item->ID, 'event-start-time', true );
45
         $filtered[end] = get_post_meta( $item->ID, 'event-end-time', true );
45
         $filtered[end] = get_post_meta( $item->ID, 'event-end-time', true );
46
+        $filtered[now] = time();
46
     }
47
     }
47
     if($item->post_type === 'exhibition') {
48
     if($item->post_type === 'exhibition') {
48
         $filtered[start] = get_post_meta( $item->ID, 'exhibit-start-date', true );
49
         $filtered[start] = get_post_meta( $item->ID, 'exhibit-start-date', true );
49
         $filtered[end] = get_post_meta( $item->ID, 'exhibit-end-date', true );
50
         $filtered[end] = get_post_meta( $item->ID, 'exhibit-end-date', true );
51
+        $filtered[now] = time();
50
     }
52
     }
51
 
53
 
52
     // Post categories and tags (store just the slugs)
54
     // Post categories and tags (store just the slugs)

Loading…
Cancel
Save