1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* [list_searcheable_acf list all the custom fields we want to include in our search query] |
5
|
|
|
* @return [array] [list of custom fields] |
|
|
|
|
6
|
|
|
*/ |
7
|
|
|
function list_searcheable_acf() |
8
|
|
|
{ |
9
|
|
|
$list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF"); |
10
|
|
|
|
11
|
|
|
return $list_searcheable_acf; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression |
16
|
|
|
* before request] |
17
|
|
|
* @param [query-part/string] $where [the initial "where" part of the search query] |
|
|
|
|
18
|
|
|
* @param [object] $wp_query [] |
|
|
|
|
19
|
|
|
* @return [query-part/string] $where [the "where" part of the search query as we customized] |
|
|
|
|
20
|
|
|
* see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/ |
21
|
|
|
* credits to Vincent Zurczak for the base query structure/spliting tags section |
22
|
|
|
*/ |
23
|
|
|
function advanced_custom_search($where, &$wp_query) |
24
|
|
|
{ |
25
|
|
|
global $wpdb; |
26
|
|
|
|
27
|
|
|
if (empty($where)) { |
28
|
|
|
return $where; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// get search expression |
32
|
|
|
$terms = $wp_query->query_vars['s']; |
33
|
|
|
|
34
|
|
|
// explode search expression to get search terms |
35
|
|
|
$exploded = explode(' ', $terms); |
36
|
|
|
|
37
|
|
|
if ($exploded === false || count($exploded) == 0) { |
38
|
|
|
$exploded = array(0 => $terms); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// reset search in order to rebuilt it as we whish |
42
|
|
|
$where = ''; |
43
|
|
|
|
44
|
|
|
// get searcheable_acf, a list of advanced custom fields you want to search content in |
45
|
|
|
$list_searcheable_acf = list_searcheable_acf(); |
46
|
|
|
|
47
|
|
|
foreach ($exploded as $tag) { |
48
|
|
|
$where .= " |
49
|
|
|
AND ( |
50
|
|
|
(wp_posts.post_title LIKE '%$tag%') |
51
|
|
|
OR (wp_posts.post_content LIKE '%$tag%') |
52
|
|
|
OR EXISTS ( |
53
|
|
|
SELECT * FROM wp_postmeta |
54
|
|
|
WHERE post_id = wp_posts.ID |
55
|
|
|
AND ("; |
56
|
|
|
|
57
|
|
|
foreach ($list_searcheable_acf as $searcheable_acf) { |
58
|
|
|
if ($searcheable_acf == $list_searcheable_acf[0]) { |
59
|
|
|
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
60
|
|
|
} else { |
61
|
|
|
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$where .= ") |
65
|
|
|
) |
66
|
|
|
OR EXISTS ( |
67
|
|
|
SELECT * FROM wp_comments |
68
|
|
|
WHERE comment_post_ID = wp_posts.ID |
69
|
|
|
AND comment_content LIKE '%$tag%' |
70
|
|
|
) |
71
|
|
|
OR EXISTS ( |
72
|
|
|
SELECT * FROM wp_terms |
73
|
|
|
INNER JOIN wp_term_taxonomy |
74
|
|
|
ON wp_term_taxonomy.term_id = wp_terms.term_id |
75
|
|
|
INNER JOIN wp_term_relationships |
76
|
|
|
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id |
77
|
|
|
WHERE ( |
78
|
|
|
taxonomy = 'post_tag' |
79
|
|
|
OR taxonomy = 'category' |
80
|
|
|
OR taxonomy = 'myCustomTax' |
81
|
|
|
) |
82
|
|
|
AND object_id = wp_posts.ID |
83
|
|
|
AND wp_terms.name LIKE '%$tag%' |
84
|
|
|
) |
85
|
|
|
)"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $where; |
89
|
|
|
} |
90
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.