-
Notifications
You must be signed in to change notification settings - Fork 12
/
include-taxonomy-in-search-wordpress.php
178 lines (147 loc) · 4.8 KB
/
include-taxonomy-in-search-wordpress.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
Author: https://www.rfmeier.net/include-category-and-post-tag-names-in-the-wordpress-search/
*/
class Simple_Taxonomy_Search {
/**
* Default constructor
*
* @since 1.0.0
*/
public function __construct() {}
/**
* Append WordPress action and filter callbacks.
*
* @see https://developer.wordpress.org/reference/functions/add_action/
*
* @since 1.0.0
*
* @return void
*/
public function append_callbacks() {
add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 );
add_filter( 'posts_where', array( $this, 'posts_where' ), 10, 2 );
add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 2 );
}
/**
* Append a JOIN clause to include taxonomy terms for a WordPress search.
*
* Callback for WordPress 'posts_join' filter.
*
* @global $wpdb
*
* @uses is_main_query()
* @see https://codex.wordpress.org/Function_Reference/is_main_query
*
* @uses is_search()
* @see https://codex.wordpress.org/Function_Reference/is_search
*
* @since 1.0.0
*
* @param string $join The SQL JOIN clause.
* @param WP_Query $query The current WP_Query object.
* @return string The SQL JOIN clause.
*/
public function posts_join( $join, $query ) {
global $wpdb;
if ( is_main_query() && is_search() ) {
$join .= "
LEFT JOIN
(
`{$wpdb->term_relationships}`
INNER JOIN
`{$wpdb->term_taxonomy}` ON `{$wpdb->term_taxonomy}`.term_taxonomy_id = `{$wpdb->term_relationships}`.term_taxonomy_id
INNER JOIN
`{$wpdb->terms}` ON `{$wpdb->terms}`.term_id = `{$wpdb->term_taxonomy}`.term_id
)
ON `{$wpdb->posts}`.ID = `{$wpdb->term_relationships}`.object_id ";
}
return $join;
}
/**
* Append a WHERE clause for the current search to include category or
* post_tag taxonomy term names.
*
* Callback for WordPress 'posts_where' filter.
*
* @see http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
*
* @global object $wpdb
*
* @since 1.0.0
*
* @param string $where The current WHERE within the SQL clause
* @param object $query The current WP_Query object
* @return string $where The possibly modified WHERE within the SQL clause
*/
public function posts_where( $where, $query ) {
global $wpdb;
if ( is_main_query() && is_search() ) {
$taxonomies = $this->posts_where_taxonomies();
$taxonomies = implode( ', ', $taxonomies );
$where .= " OR (
`{$wpdb->term_taxonomy}`.taxonomy IN( {$taxonomies} )
AND
`{$wpdb->terms}`.name LIKE '%" . esc_sql( get_query_var( 's' ) ) . "%'
)";
}
return $where;
}
/**
* Set the GROUPBY clause for post IDs if within the main query and a search.
*
* Callback for WordPress 'posts_groupby' filter.
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_groupby
*
* @global $wpdb
*
* @since 1.0.0
*
* @param string $groupby The GROUPBY sql clause.
* @param WP_Query $query The current WP_Query.
* @return string The GROUPBY sql clause.
*/
public function posts_groupby( $groupby, $query ) {
global $wpdb;
if ( is_main_query() && is_search() ) {
$groupby = "`{$wpdb->posts}`.ID";
}
return $groupby;
}
/**
* Get an array of taxonomy names used within the posts_where clause.
*
* All taxonomies will be escaped with esc_sql().
*
* @uses esc_sql()
* @see https://codex.wordpress.org/Function_Reference/esc_sql
*
* @since 1.0.0
*
* @return array An array of taxonomy names.
*/
public function posts_where_taxonomies() {
$taxonomies = array(
'category',
'post_tag',
);
/**
* Filter 'sts_posts_where_taxonomies'.
*
* Alter the taxonomies for the post where clause before they are
* formatted and escaped.
*
* @since 1.0.0
*
* @param array $taxonomies An array of taxonomy names.
*/
$taxonomies = apply_filters( 'sts_posts_where_taxonomies', $taxonomies );
foreach( $taxonomies as $index => $taxonomy ) {
$taxonomies[ $index ] = sprintf( "'%s'", esc_sql( $taxonomy ) );
}
return $taxonomies;
}
}
$stiwp = new Simple_Taxonomy_Search();
$stiwp->append_callbacks();