author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Query API |
|
4 |
* |
|
5 |
* The query API attempts to get which part of WordPress the user is on. It |
|
6 |
* also provides functionality for getting URL query information. |
|
7 |
* |
|
5 | 8 |
* @link https://codex.wordpress.org/The_Loop More information on The Loop. |
0 | 9 |
* |
10 |
* @package WordPress |
|
11 |
* @subpackage Query |
|
12 |
*/ |
|
13 |
||
14 |
/** |
|
15 |
* Retrieve variable in the WP_Query class. |
|
16 |
* |
|
17 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* @since 3.9.0 The `$default` argument was introduced. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 21 |
* |
5 | 22 |
* @param string $var The variable key to retrieve. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @param mixed $default Optional. Value to return if the query variable is not set. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* @return mixed Contents of the query variable. |
0 | 25 |
*/ |
5 | 26 |
function get_query_var( $var, $default = '' ) { |
0 | 27 |
global $wp_query; |
5 | 28 |
return $wp_query->get( $var, $default ); |
0 | 29 |
} |
30 |
||
31 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* Retrieve the currently-queried object. |
0 | 33 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
* Wrapper for WP_Query::get_queried_object(). |
0 | 35 |
* |
36 |
* @since 3.1.0 |
|
37 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @return object Queried object. |
0 | 41 |
*/ |
42 |
function get_queried_object() { |
|
43 |
global $wp_query; |
|
44 |
return $wp_query->get_queried_object(); |
|
45 |
} |
|
46 |
||
47 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* Retrieve ID of the current queried object. |
0 | 49 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* Wrapper for WP_Query::get_queried_object_id(). |
0 | 51 |
* |
52 |
* @since 3.1.0 |
|
53 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @return int ID of the queried object. |
0 | 57 |
*/ |
58 |
function get_queried_object_id() { |
|
59 |
global $wp_query; |
|
60 |
return $wp_query->get_queried_object_id(); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Set query variable. |
|
65 |
* |
|
66 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 69 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* @param string $var Query variable key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* @param mixed $value Query variable value. |
0 | 72 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
function set_query_var( $var, $value ) { |
0 | 74 |
global $wp_query; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
$wp_query->set( $var, $value ); |
0 | 76 |
} |
77 |
||
78 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* Sets up The Loop with query parameters. |
0 | 80 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* Note: This function will completely override the main query and isn't intended for use |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* by plugins or themes. Its overly-simplistic approach to modifying the main query can be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* problematic and should be avoided wherever possible. In most cases, there are better, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* more performant options for modifying the main query such as via the {@see 'pre_get_posts'} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* action within WP_Query. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* This must not be used within the WordPress Loop. |
0 | 88 |
* |
89 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 92 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* @param array|string $query Array or string of WP_Query arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* @return array List of post objects. |
0 | 95 |
*/ |
96 |
function query_posts($query) { |
|
97 |
$GLOBALS['wp_query'] = new WP_Query(); |
|
98 |
return $GLOBALS['wp_query']->query($query); |
|
99 |
} |
|
100 |
||
101 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* Destroys the previous query and sets up a new query. |
0 | 103 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* This should be used after query_posts() and before another query_posts(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
* This will remove obscure bugs that occur when the previous WP_Query object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* is not destroyed properly before another is set up. |
0 | 107 |
* |
108 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
* @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query(). |
0 | 112 |
*/ |
113 |
function wp_reset_query() { |
|
114 |
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; |
|
115 |
wp_reset_postdata(); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* After looping through a separate query, this function restores |
|
120 |
* the $post global to the current post in the main query. |
|
121 |
* |
|
122 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 125 |
*/ |
126 |
function wp_reset_postdata() { |
|
127 |
global $wp_query; |
|
5 | 128 |
|
129 |
if ( isset( $wp_query ) ) { |
|
130 |
$wp_query->reset_postdata(); |
|
131 |
} |
|
0 | 132 |
} |
133 |
||
134 |
/* |
|
135 |
* Query type checks. |
|
136 |
*/ |
|
137 |
||
138 |
/** |
|
139 |
* Is the query for an existing archive page? |
|
140 |
* |
|
141 |
* Month, Year, Category, Author, Post Type archive... |
|
142 |
* |
|
143 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 146 |
* |
147 |
* @return bool |
|
148 |
*/ |
|
149 |
function is_archive() { |
|
150 |
global $wp_query; |
|
151 |
||
152 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 154 |
return false; |
155 |
} |
|
156 |
||
157 |
return $wp_query->is_archive(); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Is the query for an existing post type archive page? |
|
162 |
* |
|
163 |
* @since 3.1.0 |
|
164 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @param string|array $post_types Optional. Post type or array of posts types to check against. |
0 | 168 |
* @return bool |
169 |
*/ |
|
170 |
function is_post_type_archive( $post_types = '' ) { |
|
171 |
global $wp_query; |
|
172 |
||
173 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 175 |
return false; |
176 |
} |
|
177 |
||
178 |
return $wp_query->is_post_type_archive( $post_types ); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Is the query for an existing attachment page? |
|
183 |
* |
|
184 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 187 |
* |
5 | 188 |
* @param int|string|array|object $attachment Attachment ID, title, slug, or array of such. |
0 | 189 |
* @return bool |
190 |
*/ |
|
5 | 191 |
function is_attachment( $attachment = '' ) { |
0 | 192 |
global $wp_query; |
193 |
||
194 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 196 |
return false; |
197 |
} |
|
198 |
||
5 | 199 |
return $wp_query->is_attachment( $attachment ); |
0 | 200 |
} |
201 |
||
202 |
/** |
|
203 |
* Is the query for an existing author archive page? |
|
204 |
* |
|
205 |
* If the $author parameter is specified, this function will additionally |
|
206 |
* check if the query is for one of the authors specified. |
|
207 |
* |
|
208 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 211 |
* |
212 |
* @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames |
|
213 |
* @return bool |
|
214 |
*/ |
|
215 |
function is_author( $author = '' ) { |
|
216 |
global $wp_query; |
|
217 |
||
218 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 220 |
return false; |
221 |
} |
|
222 |
||
223 |
return $wp_query->is_author( $author ); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Is the query for an existing category archive page? |
|
228 |
* |
|
229 |
* If the $category parameter is specified, this function will additionally |
|
230 |
* check if the query is for one of the categories specified. |
|
231 |
* |
|
232 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 235 |
* |
236 |
* @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs. |
|
237 |
* @return bool |
|
238 |
*/ |
|
239 |
function is_category( $category = '' ) { |
|
240 |
global $wp_query; |
|
241 |
||
242 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 244 |
return false; |
245 |
} |
|
246 |
||
247 |
return $wp_query->is_category( $category ); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Is the query for an existing tag archive page? |
|
252 |
* |
|
253 |
* If the $tag parameter is specified, this function will additionally |
|
254 |
* check if the query is for one of the tags specified. |
|
255 |
* |
|
256 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 259 |
* |
260 |
* @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. |
|
261 |
* @return bool |
|
262 |
*/ |
|
263 |
function is_tag( $tag = '' ) { |
|
264 |
global $wp_query; |
|
265 |
||
266 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 268 |
return false; |
269 |
} |
|
270 |
||
271 |
return $wp_query->is_tag( $tag ); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* Is the query for an existing custom taxonomy archive page? |
0 | 276 |
* |
277 |
* If the $taxonomy parameter is specified, this function will additionally |
|
278 |
* check if the query is for that specific $taxonomy. |
|
279 |
* |
|
280 |
* If the $term parameter is specified in addition to the $taxonomy parameter, |
|
281 |
* this function will additionally check if the query is for one of the terms |
|
282 |
* specified. |
|
283 |
* |
|
284 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 287 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* @param string|array $taxonomy Optional. Taxonomy slug or slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
* @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives). |
0 | 291 |
*/ |
292 |
function is_tax( $taxonomy = '', $term = '' ) { |
|
293 |
global $wp_query; |
|
294 |
||
295 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 297 |
return false; |
298 |
} |
|
299 |
||
300 |
return $wp_query->is_tax( $taxonomy, $term ); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Is the query for an existing date archive? |
|
305 |
* |
|
306 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 309 |
* |
310 |
* @return bool |
|
311 |
*/ |
|
312 |
function is_date() { |
|
313 |
global $wp_query; |
|
314 |
||
315 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 317 |
return false; |
318 |
} |
|
319 |
||
320 |
return $wp_query->is_date(); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Is the query for an existing day archive? |
|
325 |
* |
|
326 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 329 |
* |
330 |
* @return bool |
|
331 |
*/ |
|
332 |
function is_day() { |
|
333 |
global $wp_query; |
|
334 |
||
335 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 337 |
return false; |
338 |
} |
|
339 |
||
340 |
return $wp_query->is_day(); |
|
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Is the query for a feed? |
|
345 |
* |
|
346 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 349 |
* |
350 |
* @param string|array $feeds Optional feed types to check. |
|
351 |
* @return bool |
|
352 |
*/ |
|
353 |
function is_feed( $feeds = '' ) { |
|
354 |
global $wp_query; |
|
355 |
||
356 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 358 |
return false; |
359 |
} |
|
360 |
||
361 |
return $wp_query->is_feed( $feeds ); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Is the query for a comments feed? |
|
366 |
* |
|
367 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 370 |
* |
371 |
* @return bool |
|
372 |
*/ |
|
373 |
function is_comment_feed() { |
|
374 |
global $wp_query; |
|
375 |
||
376 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 378 |
return false; |
379 |
} |
|
380 |
||
381 |
return $wp_query->is_comment_feed(); |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* Is the query for the front page of the site? |
|
386 |
* |
|
387 |
* This is for what is displayed at your site's main URL. |
|
388 |
* |
|
389 |
* Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. |
|
390 |
* |
|
391 |
* If you set a static page for the front page of your site, this function will return |
|
392 |
* true when viewing that page. |
|
393 |
* |
|
394 |
* Otherwise the same as @see is_home() |
|
395 |
* |
|
396 |
* @since 2.5.0 |
|
397 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* |
0 | 400 |
* @return bool True, if front of site. |
401 |
*/ |
|
402 |
function is_front_page() { |
|
403 |
global $wp_query; |
|
404 |
||
405 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 407 |
return false; |
408 |
} |
|
409 |
||
410 |
return $wp_query->is_front_page(); |
|
411 |
} |
|
412 |
||
413 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* Determines if the query is for the blog homepage. |
0 | 415 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
* The blog homepage is the page that shows the time-based blog content of the site. |
0 | 417 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
* is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* and 'page_for_posts'. |
0 | 420 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
* If a static page is set for the front page of the site, this function will return true only |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
* on the page you set as the "Posts page". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* @since 1.5.0 |
0 | 425 |
* |
426 |
* @see is_front_page() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 428 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* @return bool True if blog view homepage, otherwise false. |
0 | 430 |
*/ |
431 |
function is_home() { |
|
432 |
global $wp_query; |
|
433 |
||
434 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 436 |
return false; |
437 |
} |
|
438 |
||
439 |
return $wp_query->is_home(); |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* Is the query for an existing month archive? |
|
444 |
* |
|
445 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 448 |
* |
449 |
* @return bool |
|
450 |
*/ |
|
451 |
function is_month() { |
|
452 |
global $wp_query; |
|
453 |
||
454 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 456 |
return false; |
457 |
} |
|
458 |
||
459 |
return $wp_query->is_month(); |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* Is the query for an existing single page? |
|
464 |
* |
|
465 |
* If the $page parameter is specified, this function will additionally |
|
466 |
* check if the query is for one of the pages specified. |
|
467 |
* |
|
468 |
* @see is_single() |
|
469 |
* @see is_singular() |
|
470 |
* |
|
471 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 474 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
* @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
* @return bool Whether the query is for an existing single page. |
0 | 477 |
*/ |
478 |
function is_page( $page = '' ) { |
|
479 |
global $wp_query; |
|
480 |
||
481 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 483 |
return false; |
484 |
} |
|
485 |
||
486 |
return $wp_query->is_page( $page ); |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* Is the query for paged result and not for the first page? |
|
491 |
* |
|
492 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 495 |
* |
496 |
* @return bool |
|
497 |
*/ |
|
498 |
function is_paged() { |
|
499 |
global $wp_query; |
|
500 |
||
501 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 503 |
return false; |
504 |
} |
|
505 |
||
506 |
return $wp_query->is_paged(); |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Is the query for a post or page preview? |
|
511 |
* |
|
512 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 515 |
* |
516 |
* @return bool |
|
517 |
*/ |
|
518 |
function is_preview() { |
|
519 |
global $wp_query; |
|
520 |
||
521 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 523 |
return false; |
524 |
} |
|
525 |
||
526 |
return $wp_query->is_preview(); |
|
527 |
} |
|
528 |
||
529 |
/** |
|
530 |
* Is the query for the robots file? |
|
531 |
* |
|
532 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 535 |
* |
536 |
* @return bool |
|
537 |
*/ |
|
538 |
function is_robots() { |
|
539 |
global $wp_query; |
|
540 |
||
541 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 543 |
return false; |
544 |
} |
|
545 |
||
546 |
return $wp_query->is_robots(); |
|
547 |
} |
|
548 |
||
549 |
/** |
|
550 |
* Is the query for a search? |
|
551 |
* |
|
552 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 555 |
* |
556 |
* @return bool |
|
557 |
*/ |
|
558 |
function is_search() { |
|
559 |
global $wp_query; |
|
560 |
||
561 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 563 |
return false; |
564 |
} |
|
565 |
||
566 |
return $wp_query->is_search(); |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Is the query for an existing single post? |
|
571 |
* |
|
572 |
* Works for any post type, except attachments and pages |
|
573 |
* |
|
574 |
* If the $post parameter is specified, this function will additionally |
|
575 |
* check if the query is for one of the Posts specified. |
|
576 |
* |
|
577 |
* @see is_page() |
|
578 |
* @see is_singular() |
|
579 |
* |
|
580 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 583 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
* @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* @return bool Whether the query is for an existing single post. |
0 | 586 |
*/ |
587 |
function is_single( $post = '' ) { |
|
588 |
global $wp_query; |
|
589 |
||
590 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 592 |
return false; |
593 |
} |
|
594 |
||
595 |
return $wp_query->is_single( $post ); |
|
596 |
} |
|
597 |
||
598 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
* Is the query for an existing single post of any post type (post, attachment, page, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
* custom post types)? |
0 | 601 |
* |
602 |
* If the $post_types parameter is specified, this function will additionally |
|
603 |
* check if the query is for one of the Posts Types specified. |
|
604 |
* |
|
605 |
* @see is_page() |
|
606 |
* @see is_single() |
|
607 |
* |
|
608 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 611 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
* @param string|array $post_types Optional. Post type or array of post types. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
* @return bool Whether the query is for an existing single post of any of the given post types. |
0 | 614 |
*/ |
615 |
function is_singular( $post_types = '' ) { |
|
616 |
global $wp_query; |
|
617 |
||
618 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 620 |
return false; |
621 |
} |
|
622 |
||
623 |
return $wp_query->is_singular( $post_types ); |
|
624 |
} |
|
625 |
||
626 |
/** |
|
627 |
* Is the query for a specific time? |
|
628 |
* |
|
629 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 632 |
* |
633 |
* @return bool |
|
634 |
*/ |
|
635 |
function is_time() { |
|
636 |
global $wp_query; |
|
637 |
||
638 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 640 |
return false; |
641 |
} |
|
642 |
||
643 |
return $wp_query->is_time(); |
|
644 |
} |
|
645 |
||
646 |
/** |
|
647 |
* Is the query for a trackback endpoint call? |
|
648 |
* |
|
649 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 652 |
* |
653 |
* @return bool |
|
654 |
*/ |
|
655 |
function is_trackback() { |
|
656 |
global $wp_query; |
|
657 |
||
658 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 660 |
return false; |
661 |
} |
|
662 |
||
663 |
return $wp_query->is_trackback(); |
|
664 |
} |
|
665 |
||
666 |
/** |
|
667 |
* Is the query for an existing year archive? |
|
668 |
* |
|
669 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 672 |
* |
673 |
* @return bool |
|
674 |
*/ |
|
675 |
function is_year() { |
|
676 |
global $wp_query; |
|
677 |
||
678 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 680 |
return false; |
681 |
} |
|
682 |
||
683 |
return $wp_query->is_year(); |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Is the query a 404 (returns no results)? |
|
688 |
* |
|
689 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 692 |
* |
693 |
* @return bool |
|
694 |
*/ |
|
695 |
function is_404() { |
|
696 |
global $wp_query; |
|
697 |
||
698 |
if ( ! isset( $wp_query ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
0 | 700 |
return false; |
701 |
} |
|
702 |
||
703 |
return $wp_query->is_404(); |
|
704 |
} |
|
705 |
||
706 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* Is the query for an embedded post? |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
709 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
710 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
713 |
* @return bool Whether we're in an embedded post or not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
function is_embed() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
global $wp_query; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
if ( ! isset( $wp_query ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
720 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
722 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
return $wp_query->is_embed(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
/** |
0 | 727 |
* Is the query the main query? |
728 |
* |
|
729 |
* @since 3.3.0 |
|
730 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
* |
0 | 733 |
* @return bool |
734 |
*/ |
|
735 |
function is_main_query() { |
|
736 |
if ( 'pre_get_posts' === current_filter() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
$message = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
/* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
'<code>pre_get_posts</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
'<code>WP_Query->is_main_query()</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
'<code>is_main_query()</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
__( 'https://codex.wordpress.org/Function_Reference/is_main_query' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
_doing_it_wrong( __FUNCTION__, $message, '3.7.0' ); |
0 | 746 |
} |
747 |
||
748 |
global $wp_query; |
|
749 |
return $wp_query->is_main_query(); |
|
750 |
} |
|
751 |
||
752 |
/* |
|
753 |
* The Loop. Post loop control. |
|
754 |
*/ |
|
755 |
||
756 |
/** |
|
757 |
* Whether current WordPress query has results to loop over. |
|
758 |
* |
|
759 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
760 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 762 |
* |
763 |
* @return bool |
|
764 |
*/ |
|
765 |
function have_posts() { |
|
766 |
global $wp_query; |
|
767 |
return $wp_query->have_posts(); |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Whether the caller is in the Loop. |
|
772 |
* |
|
773 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 776 |
* |
777 |
* @return bool True if caller is within loop, false if loop hasn't started or ended. |
|
778 |
*/ |
|
779 |
function in_the_loop() { |
|
780 |
global $wp_query; |
|
781 |
return $wp_query->in_the_loop; |
|
782 |
} |
|
783 |
||
784 |
/** |
|
785 |
* Rewind the loop posts. |
|
786 |
* |
|
787 |
* @since 1.5.0 |
|
788 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 790 |
*/ |
791 |
function rewind_posts() { |
|
792 |
global $wp_query; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
$wp_query->rewind_posts(); |
0 | 794 |
} |
795 |
||
796 |
/** |
|
797 |
* Iterate the post index in the loop. |
|
798 |
* |
|
799 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 802 |
*/ |
803 |
function the_post() { |
|
804 |
global $wp_query; |
|
805 |
$wp_query->the_post(); |
|
806 |
} |
|
807 |
||
808 |
/* |
|
809 |
* Comments loop. |
|
810 |
*/ |
|
811 |
||
812 |
/** |
|
813 |
* Whether there are comments to loop over. |
|
814 |
* |
|
815 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 818 |
* |
819 |
* @return bool |
|
820 |
*/ |
|
821 |
function have_comments() { |
|
822 |
global $wp_query; |
|
823 |
return $wp_query->have_comments(); |
|
824 |
} |
|
825 |
||
826 |
/** |
|
827 |
* Iterate comment index in the comment loop. |
|
828 |
* |
|
829 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
* @global WP_Query $wp_query Global WP_Query instance. |
0 | 832 |
* |
833 |
* @return object |
|
834 |
*/ |
|
835 |
function the_comment() { |
|
836 |
global $wp_query; |
|
837 |
return $wp_query->the_comment(); |
|
838 |
} |
|
839 |
||
840 |
/** |
|
841 |
* Redirect old slugs to the correct permalink. |
|
842 |
* |
|
843 |
* Attempts to find the current slug from the past slugs. |
|
844 |
* |
|
845 |
* @since 2.1.0 |
|
846 |
*/ |
|
847 |
function wp_old_slug_redirect() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
if ( is_404() && '' !== get_query_var( 'name' ) ) { |
0 | 849 |
// Guess the current post_type based on the query vars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
850 |
if ( get_query_var( 'post_type' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
$post_type = get_query_var( 'post_type' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
} elseif ( get_query_var( 'attachment' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
$post_type = 'attachment'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
} elseif ( get_query_var( 'pagename' ) ) { |
0 | 855 |
$post_type = 'page'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
} else { |
0 | 857 |
$post_type = 'post'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
} |
0 | 859 |
|
860 |
if ( is_array( $post_type ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
if ( count( $post_type ) > 1 ) { |
0 | 862 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
} |
5 | 864 |
$post_type = reset( $post_type ); |
0 | 865 |
} |
866 |
||
867 |
// Do not attempt redirect for hierarchical post types |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
if ( is_post_type_hierarchical( $post_type ) ) { |
0 | 869 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
} |
0 | 871 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
$id = _find_post_by_old_slug( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
if ( ! $id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
$id = _find_post_by_old_date( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
876 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
877 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
878 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
* Filters the old slug redirect post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
882 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
* @param int $id The redirect post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
$id = apply_filters( 'old_slug_redirect_post_id', $id ); |
0 | 886 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
if ( ! $id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
$link = get_permalink( $id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
892 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
if ( get_query_var( 'paged' ) > 1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
$link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
} elseif( is_embed() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
$link = user_trailingslashit( trailingslashit( $link ) . 'embed' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
897 |
} |
0 | 898 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
899 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
900 |
* Filters the old slug redirect URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
* @param string $link The redirect URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
$link = apply_filters( 'old_slug_redirect_url', $link ); |
0 | 907 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
908 |
if ( ! $link ) { |
0 | 909 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
} |
0 | 911 |
|
912 |
wp_redirect( $link, 301 ); // Permanent redirect |
|
913 |
exit; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
918 |
* Find the post ID for redirecting an old slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
* @see wp_old_slug_redirect() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
921 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
923 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
* @param string $post_type The current post type based on the query vars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
* @return int $id The Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
function _find_post_by_old_slug( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
934 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
// if year, monthnum, or day have been specified, make our query more precise |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
// just in case there are multiple identical _wp_old_slug values |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
if ( get_query_var( 'year' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
$query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
if ( get_query_var( 'monthnum' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
$query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
942 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
if ( get_query_var( 'day' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
944 |
$query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
945 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
946 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
947 |
$id = (int) $wpdb->get_var( $query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
948 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
return $id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
* Find the post ID for redirecting an old date. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
* @see wp_old_slug_redirect() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
961 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
* @param string $post_type The current post type based on the query vars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
* @return int $id The Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
function _find_post_by_old_date( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
966 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
$date_query = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
if ( get_query_var( 'year' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
$date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
if ( get_query_var( 'monthnum' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
$date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
if ( get_query_var( 'day' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
$date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
$id = 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
if ( $date_query ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
if ( ! $id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
// Check to see if an old slug matches the old date |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
return $id; |
0 | 990 |
} |
991 |
||
992 |
/** |
|
993 |
* Set up global post data. |
|
994 |
* |
|
995 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
* @since 4.4.0 Added the ability to pass a post ID to `$post`. |
0 | 997 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
* @global WP_Query $wp_query Global WP_Query instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1000 |
* @param WP_Post|object|int $post WP_Post instance or Post ID/object. |
0 | 1001 |
* @return bool True when finished. |
1002 |
*/ |
|
1003 |
function setup_postdata( $post ) { |
|
5 | 1004 |
global $wp_query; |
1005 |
||
1006 |
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) { |
|
1007 |
return $wp_query->setup_postdata( $post ); |
|
0 | 1008 |
} |
1009 |
||
5 | 1010 |
return false; |
0 | 1011 |
} |