| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- // include('formats.php');
-
- class Make_One_Each_Endpoint extends WP_REST_Controller {
- /**
- * 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, '/' . $route, [
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_all_items' )
- ),
- ]);
- }
- public function get_all_items( $request ) {
- $args = array(
- 'numberposts' => 1,
- );
-
- return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
- }
- public function prepare_all_items_for_response( $args ) {
- $collection = array();
- $types = [
- 'artist',
- 'episode',
- 'event',
- 'exhibition',
- 'guide',
- 'object',
- 'publication',
- 'technique',
- 'short',
- 'post',
- ];
-
- $q = [];
- foreach($types as $post_type) {
- array_push($q, new WP_Query(array(
- 'numberposts' => 1,
- 'post_type' => $post_type,
- 'post_status' => ['publish'],
- )));
- array_push($q, $post_type);
-
- $found_post = $q[0]->get_posts();
-
- $formatted = default_post_format( $found_post, false );
- $formatted['tried'] = $q[1];
- array_push($collection, $formatted);
- }
- wp_reset_postdata();
-
- return $collection;
- // return $types;
- }
- }
-
- ?>
|