NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

class.make-sortby.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 prepare_items_for_reponse( $items ) {
  49. $collection = array();
  50. forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, true);
  51. return $collection;
  52. }
  53. }
  54. ?>