-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSearch-in-metabox-acf.php
68 lines (63 loc) · 2.52 KB
/
Search-in-metabox-acf.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
<?php
/*Search in my ACF*/
function list_searcheable_acf(){
$list_searcheable_acf = array("email_dangky", "sdt_dangky");
return $list_searcheable_acf;
}
function advanced_custom_search( $where, $wp_query ) {
global $wpdb;
if ( empty( $where ) || $wp_query->query['post_type'] != 'tracuu_baohanh')
return $where;
// get search expression
$terms = addslashes($wp_query->query_vars[ 's' ]);
// explode search expression to get search terms
$exploded = explode( ' ', $terms );
if( $exploded === FALSE || count( $exploded ) == 0 )
$exploded = array( 0 => $terms );
// reset search in order to rebuilt it as we whish
$where = '';
// get searcheable_acf, a list of advanced custom fields you want to search content in
$list_searcheable_acf = list_searcheable_acf();
foreach( $exploded as $tag ) :
$where .= "
AND (
({$wpdb->posts}.post_title LIKE '%$tag%')
OR ({$wpdb->posts}.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM {$wpdb->postmeta}
WHERE post_id = {$wpdb->posts}.ID
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
if ($searcheable_acf == $list_searcheable_acf[0]):
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
else :
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
endif;
endforeach;
$where .= ")
)
OR EXISTS (
SELECT * FROM {$wpdb->comments}
WHERE comment_post_ID = {$wpdb->posts}.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM {$wpdb->terms}
INNER JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
INNER JOIN {$wpdb->term_relationships}
ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'myCustomTax'
)
AND object_id = {$wpdb->posts}.ID
AND {$wpdb->terms}.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
}
add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
/*#Search in my ACF*/