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' . '/' . $this->post_type . 's/' . $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 * FROM wp_posts WHERE post_type = %s AND post_status='publish' ORDER BY SUBSTRING_INDEX(post_title, ' ', -1)", $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; } } ?>