NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

class.make-sticky.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // include('formats.php');
  3. class Make_Sticky_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' => 20,
  20. 'post_type' => [
  21. 'artist',
  22. 'episode',
  23. 'event',
  24. 'exhibition',
  25. 'guide',
  26. 'object',
  27. 'publication',
  28. 'technique',
  29. 'short',
  30. 'post',
  31. 'page'
  32. ],
  33. 'meta_query' => [array(
  34. 'key' => 'is_sticky',
  35. 'value' => 'on',
  36. 'compare' => 'LIKE'
  37. )]
  38. );
  39. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  40. }
  41. public function prepare_all_items_for_response( $args ) {
  42. $collection = array();
  43. // https://developer.wordpress.org/reference/functions/get_posts/
  44. foreach( get_posts($args) as $item ) {
  45. $collection[$item->ID] = default_post_format( $item, true );
  46. }
  47. wp_reset_postdata();
  48. return $collection;
  49. }
  50. }
  51. ?>