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-sticky.php 1.4KB

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