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 prepare_items_for_reponse( $items ) { $collection = array(); forEach( $items as $key => $item ) $collection[$key] = default_post_format($item); return $collection; } } ?>