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
|
|
|
}); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.