| @@ -30,15 +30,10 @@ | ||
| 30 | 30 | $is_custom_search = $wp_query->get( 'custom_search' ) ?? false; |
| 31 | 31 | |
| 32 | 32 | if ( $is_custom_search && ! empty( $term ) ) { |
| 33 | 33 | $escaped = esc_sql( $term ); |
| 34 | - $search_in_content = $wp_query->get( self::SEARCH_IN_CONTENT_KEY ) ?? false; | |
| 35 | 34 | $search_term .= ' AND ('; |
| 36 | 35 | $search_term .= "post_title LIKE '%{$escaped}%'"; |
| 37 | - if ( $search_in_content ) { | |
| 38 | - $search_term .= " OR post_content LIKE '%{$escaped}%'"; | |
| 39 | - $search_term .= " OR post_excerpt LIKE '%{$escaped}%'"; | |
| 40 | - } | |
| 41 | 36 | if ( ctype_digit( $term ) ) { |
| 42 | 37 | $search_term .= ' OR ID = ' . intval( $term ); |
| 43 | 38 | } else { |
| 44 | 39 | $search_term .= " OR ID LIKE '%{$escaped}%'"; |
| @@ -56,8 +51,17 @@ | ||
| 56 | 51 | protected function get( \WP_REST_Request $request ) { |
| 57 | 52 | $params = $request->get_params(); |
| 58 | 53 | $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' ); |
| 59 | 54 | |
| 55 | + if ( empty( $term ) ) { | |
| 56 | + return new \WP_REST_Response( [ | |
| 57 | + 'success' => true, | |
| 58 | + 'data' => [ | |
| 59 | + 'value' => [], | |
| 60 | + ], | |
| 61 | + ], 200 ); | |
| 62 | + } | |
| 63 | + | |
| 60 | 64 | $keys_format_map = $this->filter_keys_conversion_map( |
| 61 | 65 | $params[ self::KEYS_CONVERSION_MAP_KEY ] ?? self::ALLOWED_KEYS_CONVERSION_MAP, |
| 62 | 66 | self::ALLOWED_KEYS_CONVERSION_MAP |
| 63 | 67 | ); |
| @@ -67,22 +71,18 @@ | ||
| 67 | 71 | $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true; |
| 68 | 72 | $post_types = $this->get_post_types_from_params( $request ); |
| 69 | 73 | |
| 70 | 74 | $query_args = [ |
| 71 | - 'post_type' => array_keys( $post_types ), | |
| 72 | - 'numberposts' => $post_count, | |
| 73 | - 'suppress_filters' => false, | |
| 74 | - 'custom_search' => true, | |
| 75 | - 'post_status' => $is_public_only ? 'publish' : 'any', | |
| 76 | - 'orderby' => 'modified', | |
| 77 | - 'order' => 'DESC', | |
| 75 | + 'post_type' => array_keys( $post_types ), | |
| 76 | + 'numberposts' => $post_count, | |
| 77 | + 'suppress_filters' => false, | |
| 78 | + 'custom_search' => true, | |
| 79 | + 'search_term' => $term, | |
| 80 | + 'post_status' => $is_public_only ? 'publish' : 'any', | |
| 81 | + 'orderby' => 'ID', | |
| 82 | + 'order' => 'ASC', | |
| 78 | 83 | ]; |
| 79 | 84 | |
| 80 | - if ( ! empty( $term ) ) { | |
| 81 | - $query_args['search_term'] = $term; | |
| 82 | - $query_args[ self::SEARCH_IN_CONTENT_KEY ] = $params[ self::SEARCH_IN_CONTENT_KEY ] ?? false; | |
| 83 | - } | |
| 84 | - | |
| 85 | 85 | if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) { |
| 86 | 86 | $query_args['meta_query'] = $params[ self::META_QUERY_KEY ]; |
| 87 | 87 | } |
| 88 | 88 | |
| @@ -210,14 +210,8 @@ | ||
| 210 | 210 | 'required' => false, |
| 211 | 211 | 'default' => null, |
| 212 | 212 | 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 213 | 213 | ], |
| 214 | - self::SEARCH_IN_CONTENT_KEY => [ | |
| 215 | - 'description' => 'Whether to search within post content and excerpt in addition to title', | |
| 216 | - 'type' => 'boolean', | |
| 217 | - 'required' => false, | |
| 218 | - 'default' => false, | |
| 219 | - ], | |
| 220 | 214 | ]; |
| 221 | 215 | } |
| 222 | 216 | |
| 223 | 217 | protected static function get_allowed_param_keys(): array { |
| @@ -228,9 +222,8 @@ | ||
| 228 | 222 | self::META_QUERY_KEY, |
| 229 | 223 | self::TAX_QUERY_KEY, |
| 230 | 224 | self::IS_PUBLIC_KEY, |
| 231 | 225 | self::ITEMS_COUNT_KEY, |
| 232 | - self::SEARCH_IN_CONTENT_KEY, | |
| 233 | 226 | ]; |
| 234 | 227 | } |
| 235 | 228 | |
| 236 | 229 | protected static function get_keys_to_encode(): array { |