-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsearch.php
77 lines (62 loc) · 2.36 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* SUCHFUNKTIONEN
* --------------
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Funktion zur Erweiterung der Suche
function custom_search_query($search, $wp_query) {
global $wpdb;
if (empty($search)) {
return $search; // Skip processing - no search term in query
}
// Get search term
$search_term = $wp_query->query_vars['s'];
// Skip processing if doing an admin search
if (is_admin()) {
return $search;
}
// Initial query parts for standard fields
$search = " AND (";
$search .= "{$wpdb->posts}.post_title LIKE '%{$search_term}%'";
$search .= " OR {$wpdb->posts}.post_content LIKE '%{$search_term}%'";
// Add custom meta fields to search
$all_options = amb_get_all_external_values();
$meta_keys = array_keys($all_options); // Dynamically get meta keys
// Check if search term contains specific meta key search (e.g., "audience: Lehrperson")
if (strpos($search_term, ':') !== false) {
list($specific_meta_key, $specific_meta_value) = explode(':', $search_term, 2);
$specific_meta_key = trim($specific_meta_key);
$specific_meta_value = trim($specific_meta_value);
// Ensure the specific meta key is valid
if (in_array($specific_meta_key, $meta_keys)) {
$search = " AND (";
$search .= $wpdb->prepare("EXISTS (
SELECT 1
FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
AND {$wpdb->postmeta}.meta_key = %s
AND {$wpdb->postmeta}.meta_value LIKE %s
)", $specific_meta_key, '%' . $wpdb->esc_like($specific_meta_value) . '%');
$search .= ")";
return $search;
}
}
foreach ($meta_keys as $meta_key) {
if (isset($all_options[$meta_key])) {
$search .= $wpdb->prepare(" OR EXISTS (
SELECT 1
FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
AND {$wpdb->postmeta}.meta_key = %s
AND {$wpdb->postmeta}.meta_value LIKE %s
)", $meta_key, '%' . $wpdb->esc_like($search_term) . '%');
}
}
$search .= ")";
return $search;
}
// Hook in to modify the search query
add_filter('posts_search', 'custom_search_query', 10, 2);