NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

class.make-search.php 1017B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. // include('formats.php');
  3. class Make_Search_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' => 9,
  20. );
  21. $params = $request->get_params();
  22. if($params['s']) {
  23. $args['s'] = $params['s'];
  24. }
  25. // https://www.relevanssi.com/user-manual/functions/relevanssi_do_query/
  26. $q = new WP_Query( $args );
  27. relevanssi_do_query( $q );
  28. $found_posts = $q->get_posts();
  29. return new WP_REST_Response( $found_posts, 200 );
  30. }
  31. }
  32. ?>