| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- class Make_Sort_By extends WP_REST_Controller {
- private $post_type;
- private $sort_type;
- function __construct($post_type, $sort_type) {
- $this->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;
- }
- }
-
- ?>
|