瀏覽代碼

:sparkles: using relevanssi to make custom search endpoint

tags/1.0.0
J 4 年之前
父節點
當前提交
8b51725b4a
共有 2 個文件被更改,包括 43 次插入0 次删除
  1. 7
    0
      plugins/cia-endpoints/cia-end-points.php
  2. 36
    0
      plugins/cia-endpoints/includes/class.make-search.php

+ 7
- 0
plugins/cia-endpoints/cia-end-points.php 查看文件

@@ -21,6 +21,7 @@ require_once('includes/class.make-endpoint.php');
21 21
 require_once('includes/class.make-terms.php');
22 22
 require_once('includes/class.make-sticky.php');
23 23
 require_once('includes/class.make-sortby.php');
24
+require_once('includes/class.make-search.php');
24 25
 
25 26
 function _unsnake($input) {
26 27
     return str_replace('_', '-', $input);
@@ -57,6 +58,12 @@ add_action( 'rest_api_init', function () {
57 58
      */
58 59
     $terms_controller = new Make_Terms_Endpoint();
59 60
     $terms_controller->register_custom_route('terms');
61
+    
62
+    /**
63
+     * Search endpoint
64
+     */
65
+    $search_controller = new Make_Search_Endpoint();
66
+    $search_controller->register_custom_route('search');
60 67
 
61 68
     /**
62 69
      * Craft in America custom sort_types

+ 36
- 0
plugins/cia-endpoints/includes/class.make-search.php 查看文件

@@ -0,0 +1,36 @@
1
+<?php
2
+// include('formats.php');
3
+
4
+class Make_Search_Endpoint extends WP_REST_Controller {
5
+    /**
6
+     * Register the routes for the objects of the controller.
7
+     */
8
+    public function register_custom_route($route) {
9
+        $version = '2';
10
+        $namespace = 'craft/v' . $version;
11
+
12
+        register_rest_route( $namespace, '/' . $route, [
13
+            array(
14
+                'methods'  => WP_REST_Server::READABLE,
15
+                'callback' => array( $this, 'get_all_items' )
16
+            ),
17
+        ]);
18
+    }
19
+    public function get_all_items( $request ) {
20
+        $args = array(
21
+            'numberposts' => 9,
22
+        );
23
+        $params = $request->get_params();
24
+        if($params['s']) {
25
+            $args['s'] = $params['s'];
26
+        }
27
+        // https://www.relevanssi.com/user-manual/functions/relevanssi_do_query/
28
+        $q = new WP_Query( $args );
29
+        relevanssi_do_query( $q );
30
+        $found_posts = $q->get_posts();
31
+        
32
+        return new WP_REST_Response( $found_posts, 200 );
33
+    }
34
+}
35
+
36
+?>

Loading…
取消
儲存