|
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
|
|
|
return $list_searcheable_acf; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression before request] |
|
15
|
|
|
* @param [query-part/string] $where [the initial "where" part of the search query] |
|
|
|
|
|
|
16
|
|
|
* @param [object] $wp_query [] |
|
|
|
|
|
|
17
|
|
|
* @return [query-part/string] $where [the "where" part of the search query as we customized] |
|
|
|
|
|
|
18
|
|
|
* see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/ |
|
19
|
|
|
* credits to Vincent Zurczak for the base query structure/spliting tags section |
|
20
|
|
|
*/ |
|
21
|
|
|
function advanced_custom_search($where, &$wp_query) |
|
22
|
|
|
{ |
|
23
|
|
|
global $wpdb; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if (empty($where)) { |
|
26
|
|
|
return $where; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// get search expression |
|
30
|
|
|
$terms = $wp_query->query_vars['s']; |
|
31
|
|
|
|
|
32
|
|
|
// explode search expression to get search terms |
|
33
|
|
|
$exploded = explode(' ', $terms); |
|
34
|
|
|
|
|
35
|
|
|
if ($exploded === FALSE || count($exploded) == 0) { |
|
36
|
|
|
$exploded = array(0 => $terms); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// reset search in order to rebuilt it as we whish |
|
40
|
|
|
$where = ''; |
|
41
|
|
|
|
|
42
|
|
|
// get searcheable_acf, a list of advanced custom fields you want to search content in |
|
43
|
|
|
$list_searcheable_acf = list_searcheable_acf(); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($exploded as $tag) : |
|
46
|
|
|
$where .= " |
|
47
|
|
|
AND ( |
|
48
|
|
|
(wp_posts.post_title LIKE '%$tag%') |
|
49
|
|
|
OR (wp_posts.post_content LIKE '%$tag%') |
|
50
|
|
|
OR EXISTS ( |
|
51
|
|
|
SELECT * FROM wp_postmeta |
|
52
|
|
|
WHERE post_id = wp_posts.ID |
|
53
|
|
|
AND ("; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($list_searcheable_acf as $searcheable_acf) : |
|
56
|
|
|
if ($searcheable_acf == $list_searcheable_acf[0]): |
|
57
|
|
|
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
|
58
|
|
|
else : |
|
59
|
|
|
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
|
60
|
|
|
endif; |
|
61
|
|
|
endforeach; |
|
62
|
|
|
|
|
63
|
|
|
$where .= ") |
|
64
|
|
|
) |
|
65
|
|
|
OR EXISTS ( |
|
66
|
|
|
SELECT * FROM wp_comments |
|
67
|
|
|
WHERE comment_post_ID = wp_posts.ID |
|
68
|
|
|
AND comment_content LIKE '%$tag%' |
|
69
|
|
|
) |
|
70
|
|
|
OR EXISTS ( |
|
71
|
|
|
SELECT * FROM wp_terms |
|
72
|
|
|
INNER JOIN wp_term_taxonomy |
|
73
|
|
|
ON wp_term_taxonomy.term_id = wp_terms.term_id |
|
74
|
|
|
INNER JOIN wp_term_relationships |
|
75
|
|
|
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id |
|
76
|
|
|
WHERE ( |
|
77
|
|
|
taxonomy = 'post_tag' |
|
78
|
|
|
OR taxonomy = 'category' |
|
79
|
|
|
OR taxonomy = 'myCustomTax' |
|
80
|
|
|
) |
|
81
|
|
|
AND object_id = wp_posts.ID |
|
82
|
|
|
AND wp_terms.name LIKE '%$tag%' |
|
83
|
|
|
) |
|
84
|
|
|
)"; |
|
85
|
|
|
endforeach; |
|
86
|
|
|
|
|
87
|
|
|
return $where; |
|
88
|
|
|
} |
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.