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-oneofeach.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // include('formats.php');
  3. class Make_One_Each_Endpoint extends WP_REST_Controller {
  4. /**
  5. * Register the routes for the objects of the controller.
  6. */
  7. public function register_custom_route($route) {
  8. $version = '2';
  9. $namespace = 'craft/v' . $version;
  10. register_rest_route( $namespace, '/' . $route, [
  11. array(
  12. 'methods' => WP_REST_Server::READABLE,
  13. 'callback' => array( $this, 'get_all_items' )
  14. ),
  15. ]);
  16. }
  17. public function get_all_items( $request ) {
  18. $args = array(
  19. 'numberposts' => 1,
  20. );
  21. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  22. }
  23. public function prepare_all_items_for_response( $args ) {
  24. $collection = array();
  25. $types = [
  26. 'artist',
  27. 'episode',
  28. 'event',
  29. 'exhibition',
  30. 'guide',
  31. 'object',
  32. 'publication',
  33. 'technique',
  34. 'short',
  35. 'post',
  36. ];
  37. $q = [];
  38. foreach($types as $post_type) {
  39. array_push($q, new WP_Query(array(
  40. 'numberposts' => 1,
  41. 'post_type' => $post_type,
  42. 'post_status' => ['publish'],
  43. )));
  44. array_push($q, $post_type);
  45. $found_post = $q[0]->get_posts();
  46. $formatted = default_post_format( $found_post, false );
  47. $formatted['tried'] = $q[1];
  48. array_push($collection, $formatted);
  49. }
  50. wp_reset_postdata();
  51. return $collection;
  52. // return $types;
  53. }
  54. }
  55. ?>