NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

class.make-sortby.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. class Make_Sort_By extends WP_REST_Controller {
  3. private $post_type;
  4. private $sort_type;
  5. function __construct($post_type, $sort_type) {
  6. $this->post_type = $post_type;
  7. $this->sort_type = $sort_type;
  8. }
  9. /**
  10. * Register the routes for the objects of the controller.
  11. */
  12. public function register_custom_route($route) {
  13. $version = '2';
  14. $namespace = 'craft/v' . $version;
  15. register_rest_route( $namespace, '/sort' . '/' . $this->post_type . 's/' . $route, [
  16. array(
  17. 'methods' => WP_REST_Server::READABLE,
  18. 'callback' => array( $this, $this->sort_type )
  19. ),
  20. ]);
  21. }
  22. public function by_alpha( $request ) {
  23. global $wpdb;
  24. $res = $wpdb->get_results($wpdb->prepare(
  25. "SELECT * FROM wp_posts
  26. WHERE post_type = %s
  27. AND post_status='publish'
  28. ORDER BY SUBSTRING_INDEX(post_title, ' ', -1)",
  29. $this->post_type
  30. ));
  31. wp_reset_postdata();
  32. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  33. }
  34. public function by_material( $request ) {
  35. global $wpdb;
  36. // !: Make this a real query
  37. $res = $wpdb->get_results($wpdb->prepare(
  38. "SELECT * FROM wp_posts
  39. WHERE post_type = %s
  40. AND post_status='publish'",
  41. $this->post_type
  42. ));
  43. wp_reset_postdata();
  44. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  45. }
  46. public function prepare_items_for_reponse( $items ) {
  47. $collection = array();
  48. forEach( $items as $key => $item ) $collection[$key] = default_post_format($item);
  49. return $collection;
  50. }
  51. }
  52. ?>