|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Undefined\Stash\ACF; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Get paths for assets |
|
7
|
|
|
*/ |
|
8
|
|
|
class ACF |
|
9
|
|
|
{ |
|
10
|
|
|
function __construct() |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
$this->list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF"); |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
if (!class_exists('acf')) { |
|
15
|
|
|
add_filter('posts_search', [$this, 'advanced_custom_search'], 500, 2); |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Make ACF fields searchable |
|
21
|
|
|
* |
|
22
|
|
|
* @param $where |
|
23
|
|
|
* @param $wp_query |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function advanced_custom_search($where, &$wp_query) |
|
27
|
|
|
{ |
|
28
|
|
|
global $wpdb; |
|
29
|
|
|
|
|
30
|
|
|
if (empty($where)) { |
|
31
|
|
|
return $where; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// get search expression |
|
35
|
|
|
$terms = $wp_query->query_vars['s']; |
|
36
|
|
|
|
|
37
|
|
|
// explode search expression to get search terms |
|
38
|
|
|
$exploded = explode(' ', $terms); |
|
39
|
|
|
|
|
40
|
|
|
if ($exploded === false || count($exploded) == 0) { |
|
41
|
|
|
$exploded = array(0 => $terms); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// reset search in order to rebuilt it as we whish |
|
45
|
|
|
$where = ''; |
|
46
|
|
|
|
|
47
|
|
|
// get searcheable_acf, a list of advanced custom fields you want to search content in |
|
48
|
|
|
$list_searcheable_acf = $this->list_searcheable_acf; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($exploded as $tag) { |
|
51
|
|
|
$where .= " |
|
52
|
|
|
AND ( |
|
53
|
|
|
(wp_posts.post_title LIKE '%$tag%') |
|
54
|
|
|
OR (wp_posts.post_content LIKE '%$tag%') |
|
55
|
|
|
OR EXISTS ( |
|
56
|
|
|
SELECT * FROM wp_postmeta |
|
57
|
|
|
WHERE post_id = wp_posts.ID |
|
58
|
|
|
AND ("; |
|
59
|
|
|
|
|
60
|
|
|
foreach ($list_searcheable_acf as $searcheable_acf) { |
|
61
|
|
|
if ($searcheable_acf == $list_searcheable_acf[0]) { |
|
62
|
|
|
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
|
63
|
|
|
} else { |
|
64
|
|
|
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$where .= ") |
|
68
|
|
|
) |
|
69
|
|
|
OR EXISTS ( |
|
70
|
|
|
SELECT * FROM wp_comments |
|
71
|
|
|
WHERE comment_post_ID = wp_posts.ID |
|
72
|
|
|
AND comment_content LIKE '%$tag%' |
|
73
|
|
|
) |
|
74
|
|
|
OR EXISTS ( |
|
75
|
|
|
SELECT * FROM wp_terms |
|
76
|
|
|
INNER JOIN wp_term_taxonomy |
|
77
|
|
|
ON wp_term_taxonomy.term_id = wp_terms.term_id |
|
78
|
|
|
INNER JOIN wp_term_relationships |
|
79
|
|
|
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id |
|
80
|
|
|
WHERE ( |
|
81
|
|
|
taxonomy = 'post_tag' |
|
82
|
|
|
OR taxonomy = 'category' |
|
83
|
|
|
OR taxonomy = 'myCustomTax' |
|
84
|
|
|
) |
|
85
|
|
|
AND object_id = wp_posts.ID |
|
86
|
|
|
AND wp_terms.name LIKE '%$tag%' |
|
87
|
|
|
) |
|
88
|
|
|
)"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $where; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
add_action('init', function () { |
|
97
|
|
|
$acf = new ACF(); |
|
|
|
|
|
|
98
|
|
|
}); |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.