NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

class.make-sticky.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 'short',
  29. 'post',
  30. 'page'
  31. ],
  32. 'meta_query' => [array(
  33. 'key' => 'is_sticky',
  34. 'value' => 'on',
  35. 'compare' => 'LIKE'
  36. )]
  37. );
  38. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  39. }
  40. public function prepare_all_items_for_response( $args ) {
  41. $collection = array();
  42. // https://developer.wordpress.org/reference/functions/get_posts/
  43. foreach( get_posts($args) as $item ) {
  44. $collection[$item->ID] = default_post_format( $item, true );
  45. }
  46. wp_reset_postdata();
  47. return $collection;
  48. }
  49. }
  50. ?>