-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.php
More file actions
268 lines (242 loc) · 8.86 KB
/
query.php
File metadata and controls
268 lines (242 loc) · 8.86 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* Rewrite the archive and category page queries to filter and order the posts
* correctly.
*
* @return void
*/
add_filter(
'pre_get_posts',
function ($query) {
// Filter applies to front end queries only.
if (is_admin()) {
return;
}
// Apply the filter to the main query.
if ($query->is_main_query()) {
if (is_post_type_archive(CGIT_EVENTS_POST_TYPE) && !is_tax(CGIT_EVENTS_POST_TYPE_CATEGORY)) {
// Adjust the query for archives and main event listings
cgit_wp_events_query_archive($query);
} elseif (is_tax(CGIT_EVENTS_POST_TYPE_CATEGORY)) {
// Adjust the query for category listings
global $wp_query;
$term = $wp_query->get_queried_object();
if ($term
&& $term->taxonomy == CGIT_EVENTS_POST_TYPE_CATEGORY
) {
cgit_wp_events_query_main_listing($query);
}
}
}
}
);
/**
* Remove the default year/month and day archive WHERE clauses. We use the date
* query vars to query data by the event date instead of publish date.
*/
add_filter('posts_where', function ($where, $query) {
// Filter applies to front end queries only.
if (is_admin()) {
return $where;
}
// Apply the filter to the main query.
if ($query->is_main_query() || ($query->query['facetwp'] ?? false)) {
if (is_post_type_archive(CGIT_EVENTS_POST_TYPE)) {
// Year archive
if (get_query_var('year', null)
&& get_query_var('monthnum', null) === 0
) {
$regex = '/\s*AND\s*\(\s*YEAR\s*\([^\)]+?\)\s*=\s*\d+\s*\)/';
$where = preg_replace($regex, '', $where);
}
// Monthly archive
if (get_query_var('year', null)
&& get_query_var('monthnum', null)
&& get_query_var('day', null) === 0
) {
$regex = '/\s*AND\s*\(\s*\(\s*YEAR\s*\([^\)]+?\)\s*=\s*\d+\s*AND\s*MONTH\s*\([^\)]+?\)\s*=\s*\d+\s*\)\s*\)/';
$where = preg_replace($regex, '', $where);
}
// Day archive
if (get_query_var('year', null)
&& get_query_var('monthnum', null)
&& get_query_var('day', null)
) {
$regex = '/\s*AND\s*\(\s*\(\s*YEAR\s*\([^\)]+?\)\s*=\s*\d+\s*AND\s*MONTH\s*\([^\)]+?\)\s*=\s*\d+\s*AND\s*DAYOFMONTH\s*\([^\)]+?\)\s*=\s*\d+\s*\)\s*\)/';
$where = preg_replace($regex, '', $where);
}
}
}
return $where;
}, 10, 2);
/**
* Rewrite the events archive page SQL query. WordPress assumes the dates in the
* URL are to show standard post archives by date. These are disabled and custom
* queries generated to check against the meta values that the start and end
* dates are stored in.
*
* @return void
*/
function cgit_wp_events_query_archive($query)
{
// Get the dates from query vars
$year = get_query_var('year', null);
$month = get_query_var('monthnum', null);
$day = get_query_var('day', null);
$has_year = !empty($year);
$has_month = !empty($month);
$has_day = !empty($day);
$date = (new DateTime())->setDate($year, $month, (empty($day) ? 1 : $day));
define('CGIT_WP_EVENTS_YEAR', $date->format('Y'));
define('CGIT_WP_EVENTS_MONTH', $date->format('n'));
define('CGIT_WP_EVENTS_DAY', $date->format('j'));
$meta_date_format = 'Ymd';
$meta_date_format = apply_filters('cgit_wp_acf_events_meta_date_format', $meta_date_format);
if ($has_year && $has_month && $has_day) {
// Displaying a single day archive
$query->set('meta_query', array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $date->format($meta_date_format),
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'end_date',
'value' => $date->format($meta_date_format),
'type' => 'NUMERIC',
'compare' => '>='
)
));
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
} elseif ($has_year && $has_month) {
// Number of days in this month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// Start of month
$month_start = (new DateTime())->setDate($year, $month, 1);
// End of the month
$month_end = (new DateTime())->setDate($year, $month, $days_in_month);
// Month archive
$query->set('meta_query', array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => [$month_start->format($meta_date_format), $month_end->format($meta_date_format)],
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
array(
'key' => 'end_date',
'value' => [$month_start->format($meta_date_format), $month_end->format($meta_date_format)],
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
),
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $month_start->format($meta_date_format),
'type' => 'NUMERIC',
'compare' => '<'
),
array(
'key' => 'end_date',
'value' => $month_end->format($meta_date_format),
'type' => 'NUMERIC',
'compare' => '>'
)
),
));
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
} elseif ($has_year) {
// Start of the year
$year_start = (new DateTime())->setDate($year, 1, 1);
// End of the year
$year_end = (new DateTime())->setDate($year, 12, 31);
// Year archive
$query->set('meta_query', array(
'relation' => 'OR',
array(
'relation' => 'OR',
'start_date' => array(
'key' => 'start_date',
'value' => [$year_start->format('Ymd'), $year_end->format('Ymd')],
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
'end_date' => array(
'key' => 'end_date',
'value' => [$year_start->format('Ymd'), $year_end->format('Ymd')],
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
),
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $year_start->format('Ymd'),
'type' => 'NUMERIC',
'compare' => '<'
),
array(
'key' => 'end_date',
'value' => $year_end->format('Ymd'),
'type' => 'NUMERIC',
'compare' => '>'
)
),
));
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
} else {
// This is the main listing of events
cgit_wp_events_query_main_listing($query);
}
}
/**
* Rewrite the category listings. Join with the meta tables so we can order the
* results by start_date.
*
* @return void
*/
function cgit_wp_events_query_main_listing($query)
{
$now = new DateTime('now');
$compare = (new DateTime('now'))->modify('+900 years');
$meta_date_format = 'Ymd';
$meta_date_format = apply_filters('cgit_wp_acf_events_meta_date_format', $meta_date_format);
/**
* Where start_date is greater than one. This is here purely to force a join
* on the meta tables without manually overwriting the join.
*/
$query->set('meta_query',
array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $now->format($meta_date_format),
'type' => 'DATE',
'compare' => '>='
),
'order_by_clause' => array(
'key' => 'start_date',
'value' => $compare->format($meta_date_format),
'type' => 'DATE',
'compare' => '!='
),
)
);
// Order by start date
$query->set('orderby', 'order_by_clause');
$query->set('order', 'ASC');
}