NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class.make-sortby.php 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/' . $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 DISTINCT wp_posts.*,
  26. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  27. FROM wp_posts LEFT JOIN wp_postmeta
  28. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  29. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  30. ORDER BY sort_name",
  31. $this->post_type
  32. ));
  33. wp_reset_postdata();
  34. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  35. }
  36. public function by_material( $request ) {
  37. global $wpdb;
  38. // !: Make this a real query
  39. $res = $wpdb->get_results($wpdb->prepare(
  40. "SELECT * FROM wp_posts
  41. WHERE post_type = %s
  42. AND post_status='publish'",
  43. $this->post_type
  44. ));
  45. wp_reset_postdata();
  46. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  47. }
  48. public function by_upcoming_event( $request ) {
  49. global $wpdb;
  50. $time = new DateTime();
  51. $timestamp = $time->getTimestamp();
  52. $res = $wpdb->get_results($wpdb->prepare(
  53. "SELECT DISTINCT wp_posts.*
  54. FROM wp_posts LEFT JOIN wp_postmeta AS ends
  55. ON (ends.post_id = wp_posts.ID AND ends.meta_key='event-end-time' AND ends.meta_value <= %d)
  56. WHERE post_type = %s
  57. AND CONVERT('event-end-time', DATETIME) >= CONVERT('2021-10-31', DATETIME)
  58. AND post_status='publish'
  59. ORDER BY ends.meta_value DESC",
  60. $time, $this->post_type
  61. ));
  62. wp_reset_postdata();
  63. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  64. }
  65. public function by_upcoming_exhibition( $request ) {
  66. global $wpdb;
  67. $res = $wpdb->get_results($wpdb->prepare(
  68. "SELECT DISTINCT wp_posts.*
  69. FROM wp_posts LEFT JOIN wp_postmeta
  70. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='exhibit-end-date')
  71. WHERE post_type = 'exhibition'
  72. AND post_status='publish'
  73. ORDER BY 'exhibit-end-date'",
  74. $this->post_type
  75. ));
  76. wp_reset_postdata();
  77. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  78. }
  79. public function prepare_items_for_reponse( $items ) {
  80. $collection = array();
  81. forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, true);
  82. return $collection;
  83. }
  84. }
  85. ?>