post_type = $post_type; $this->sort_type = $sort_type; } /** * Register the routes for the objects of the controller. */ public function register_custom_route($route) { $version = '2'; $namespace = 'craft/v' . $version; register_rest_route( $namespace, '/sort/' . $route, [ array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, $this->sort_type ) ), ]); } public function by_alpha( $request ) { global $wpdb; $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.*, IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name FROM wp_posts LEFT JOIN wp_postmeta ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name') WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish' ORDER BY sort_name", $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function by_material( $request ) { global $wpdb; // !: Make this a real query $res = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_posts WHERE post_type = %s AND post_status = 'publish'", $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function by_past( $request ) { global $wpdb; $time = strval(time()); $end = 'exhibit-end-date'; if($this->post_type == 'event') { $end = 'event-end-time'; } $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.* FROM wp_posts JOIN wp_postmeta AS ends ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value <= %s) WHERE post_type = %s AND post_status = 'publish' ORDER BY ends.meta_value DESC", $end, $time, $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function by_current( $request ) { global $wpdb; $time = strval(time()); $start = 'exhibit-start-date'; $end = 'exhibit-end-date'; if($this->post_type == 'event') { $start = 'event-start-time'; $end = 'event-end-time'; } $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.* FROM wp_posts JOIN wp_postmeta AS starts ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s) JOIN wp_postmeta AS ends ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s) WHERE post_type = %s AND post_status = 'publish' ORDER BY starts.meta_value DESC", $start, $time, $end, $time, $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function by_current_and_upcoming( $request ) { global $wpdb; $time = strval(time()); $start = 'exhibit-start-date'; $end = 'exhibit-end-date'; if($this->post_type == 'event') { $start = 'event-start-time'; $end = 'event-end-time'; } $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.* FROM wp_posts JOIN wp_postmeta AS starts ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s OR starts.meta_value > %s) JOIN wp_postmeta AS ends ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s) WHERE post_type = %s AND post_status = 'publish' ORDER BY ends.meta_value ASC", $start, $time, $time, $end, $time, $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function by_upcoming( $request ) { global $wpdb; $time = strval(time()); $start = 'exhibit-start-date'; if($this->post_type == 'event') { $start = 'event-start-time'; } $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.* FROM wp_posts JOIN wp_postmeta AS starts ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value >= %s) WHERE post_type = %s AND post_status = 'publish' ORDER BY ends.meta_value ASC", $start, $time, $this->post_type )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 ); } public function prepare_items_for_reponse( $items ) { $collection = array(); forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, false); return $collection; } } ?>