NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

class.make-terms.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. // include('formats.php');
  3. class Make_Terms_Endpoint extends WP_REST_Controller {
  4. function make_args($request, $post_type) {
  5. $args = [
  6. 'post_type' => $post_type,
  7. 'post_status' => ['publish'],
  8. 'posts_per_page' => -1,
  9. 'page' => 1
  10. ];
  11. }
  12. /**
  13. * Register the routes for the objects of the controller.
  14. */
  15. public function register_custom_route($route) {
  16. $version = '2';
  17. $namespace = 'craft/v' . $version;
  18. register_rest_route( $namespace, '/' . $route, [
  19. array(
  20. 'methods' => WP_REST_Server::READABLE,
  21. 'callback' => array( $this, 'get_all_terms' )
  22. ),
  23. ]);
  24. }
  25. public function get_all_terms( $request ) {
  26. $terms = $categories = get_terms($post->post_type . '_type', array(
  27. 'orderby' => 'count',
  28. 'hide_empty' => 0,
  29. ));
  30. $taxonomies = get_object_taxonomies($post->post_type);
  31. $formatted[terms] = $terms;
  32. $formatted[taxonomies] = $taxonomies;
  33. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  34. }
  35. public function prepare_all_items_for_response( $args ) {
  36. $collection = [];
  37. // https://developer.wordpress.org/reference/functions/get_posts/
  38. foreach( get_posts($args) as $item ) {
  39. $formatted = default_post_format( $item, true );
  40. array_push($collection, $formatted);
  41. }
  42. wp_reset_postdata();
  43. return $collection;
  44. }
  45. }
  46. ?>