WordPress 3.8
[autoinstalls/wordpress.git] / wp-includes / query.php
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  *
8  * @link http://codex.wordpress.org/The_Loop More information on The Loop.
9  *
10  * @package WordPress
11  * @subpackage Query
12  */
13
14 /**
15  * Retrieve variable in the WP_Query class.
16  *
17  * @see WP_Query::get()
18  * @since 1.5.0
19  * @uses $wp_query
20  *
21  * @param string $var The variable key to retrieve.
22  * @return mixed
23  */
24 function get_query_var($var) {
25         global $wp_query;
26
27         return $wp_query->get($var);
28 }
29
30 /**
31  * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
32  *
33  * @uses WP_Query::get_queried_object
34  *
35  * @since 3.1.0
36  * @access public
37  *
38  * @return object
39  */
40 function get_queried_object() {
41         global $wp_query;
42         return $wp_query->get_queried_object();
43 }
44
45 /**
46  * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
47  *
48  * @uses WP_Query::get_queried_object_id()
49  *
50  * @since 3.1.0
51  * @access public
52  *
53  * @return int
54  */
55 function get_queried_object_id() {
56         global $wp_query;
57         return $wp_query->get_queried_object_id();
58 }
59
60 /**
61  * Set query variable.
62  *
63  * @see WP_Query::set()
64  * @since 2.2.0
65  * @uses $wp_query
66  *
67  * @param string $var Query variable key.
68  * @param mixed $value
69  * @return null
70  */
71 function set_query_var($var, $value) {
72         global $wp_query;
73
74         return $wp_query->set($var, $value);
75 }
76
77 /**
78  * Set up The Loop with query parameters.
79  *
80  * This will override the current WordPress Loop and shouldn't be used more than
81  * once. This must not be used within the WordPress Loop.
82  *
83  * @since 1.5.0
84  * @uses $wp_query
85  *
86  * @param string $query
87  * @return array List of posts
88  */
89 function query_posts($query) {
90         $GLOBALS['wp_query'] = new WP_Query();
91         return $GLOBALS['wp_query']->query($query);
92 }
93
94 /**
95  * Destroy the previous query and set up a new query.
96  *
97  * This should be used after {@link query_posts()} and before another {@link
98  * query_posts()}. This will remove obscure bugs that occur when the previous
99  * wp_query object is not destroyed properly before another is set up.
100  *
101  * @since 2.3.0
102  * @uses $wp_query
103  */
104 function wp_reset_query() {
105         $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
106         wp_reset_postdata();
107 }
108
109 /**
110  * After looping through a separate query, this function restores
111  * the $post global to the current post in the main query.
112  *
113  * @since 3.0.0
114  * @uses $wp_query
115  */
116 function wp_reset_postdata() {
117         global $wp_query;
118         $wp_query->reset_postdata();
119 }
120
121 /*
122  * Query type checks.
123  */
124
125 /**
126  * Is the query for an existing archive page?
127  *
128  * Month, Year, Category, Author, Post Type archive...
129  *
130  * @see WP_Query::is_archive()
131  * @since 1.5.0
132  * @uses $wp_query
133  *
134  * @return bool
135  */
136 function is_archive() {
137         global $wp_query;
138
139         if ( ! isset( $wp_query ) ) {
140                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
141                 return false;
142         }
143
144         return $wp_query->is_archive();
145 }
146
147 /**
148  * Is the query for an existing post type archive page?
149  *
150  * @see WP_Query::is_post_type_archive()
151  * @since 3.1.0
152  * @uses $wp_query
153  *
154  * @param mixed $post_types Optional. Post type or array of posts types to check against.
155  * @return bool
156  */
157 function is_post_type_archive( $post_types = '' ) {
158         global $wp_query;
159
160         if ( ! isset( $wp_query ) ) {
161                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
162                 return false;
163         }
164
165         return $wp_query->is_post_type_archive( $post_types );
166 }
167
168 /**
169  * Is the query for an existing attachment page?
170  *
171  * @see WP_Query::is_attachment()
172  * @since 2.0.0
173  * @uses $wp_query
174  *
175  * @return bool
176  */
177 function is_attachment() {
178         global $wp_query;
179
180         if ( ! isset( $wp_query ) ) {
181                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
182                 return false;
183         }
184
185         return $wp_query->is_attachment();
186 }
187
188 /**
189  * Is the query for an existing author archive page?
190  *
191  * If the $author parameter is specified, this function will additionally
192  * check if the query is for one of the authors specified.
193  *
194  * @see WP_Query::is_author()
195  * @since 1.5.0
196  * @uses $wp_query
197  *
198  * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
199  * @return bool
200  */
201 function is_author( $author = '' ) {
202         global $wp_query;
203
204         if ( ! isset( $wp_query ) ) {
205                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
206                 return false;
207         }
208
209         return $wp_query->is_author( $author );
210 }
211
212 /**
213  * Is the query for an existing category archive page?
214  *
215  * If the $category parameter is specified, this function will additionally
216  * check if the query is for one of the categories specified.
217  *
218  * @see WP_Query::is_category()
219  * @since 1.5.0
220  * @uses $wp_query
221  *
222  * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
223  * @return bool
224  */
225 function is_category( $category = '' ) {
226         global $wp_query;
227
228         if ( ! isset( $wp_query ) ) {
229                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
230                 return false;
231         }
232
233         return $wp_query->is_category( $category );
234 }
235
236 /**
237  * Is the query for an existing tag archive page?
238  *
239  * If the $tag parameter is specified, this function will additionally
240  * check if the query is for one of the tags specified.
241  *
242  * @see WP_Query::is_tag()
243  * @since 2.3.0
244  * @uses $wp_query
245  *
246  * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
247  * @return bool
248  */
249 function is_tag( $tag = '' ) {
250         global $wp_query;
251
252         if ( ! isset( $wp_query ) ) {
253                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
254                 return false;
255         }
256
257         return $wp_query->is_tag( $tag );
258 }
259
260 /**
261  * Is the query for an existing taxonomy archive page?
262  *
263  * If the $taxonomy parameter is specified, this function will additionally
264  * check if the query is for that specific $taxonomy.
265  *
266  * If the $term parameter is specified in addition to the $taxonomy parameter,
267  * this function will additionally check if the query is for one of the terms
268  * specified.
269  *
270  * @see WP_Query::is_tax()
271  * @since 2.5.0
272  * @uses $wp_query
273  *
274  * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
275  * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
276  * @return bool
277  */
278 function is_tax( $taxonomy = '', $term = '' ) {
279         global $wp_query;
280
281         if ( ! isset( $wp_query ) ) {
282                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
283                 return false;
284         }
285
286         return $wp_query->is_tax( $taxonomy, $term );
287 }
288
289 /**
290  * Whether the current URL is within the comments popup window.
291  *
292  * @see WP_Query::is_comments_popup()
293  * @since 1.5.0
294  * @uses $wp_query
295  *
296  * @return bool
297  */
298 function is_comments_popup() {
299         global $wp_query;
300
301         if ( ! isset( $wp_query ) ) {
302                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
303                 return false;
304         }
305
306         return $wp_query->is_comments_popup();
307 }
308
309 /**
310  * Is the query for an existing date archive?
311  *
312  * @see WP_Query::is_date()
313  * @since 1.5.0
314  * @uses $wp_query
315  *
316  * @return bool
317  */
318 function is_date() {
319         global $wp_query;
320
321         if ( ! isset( $wp_query ) ) {
322                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
323                 return false;
324         }
325
326         return $wp_query->is_date();
327 }
328
329 /**
330  * Is the query for an existing day archive?
331  *
332  * @see WP_Query::is_day()
333  * @since 1.5.0
334  * @uses $wp_query
335  *
336  * @return bool
337  */
338 function is_day() {
339         global $wp_query;
340
341         if ( ! isset( $wp_query ) ) {
342                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
343                 return false;
344         }
345
346         return $wp_query->is_day();
347 }
348
349 /**
350  * Is the query for a feed?
351  *
352  * @see WP_Query::is_feed()
353  * @since 1.5.0
354  * @uses $wp_query
355  *
356  * @param string|array $feeds Optional feed types to check.
357  * @return bool
358  */
359 function is_feed( $feeds = '' ) {
360         global $wp_query;
361
362         if ( ! isset( $wp_query ) ) {
363                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
364                 return false;
365         }
366
367         return $wp_query->is_feed( $feeds );
368 }
369
370 /**
371  * Is the query for a comments feed?
372  *
373  * @see WP_Query::is_comments_feed()
374  * @since 3.0.0
375  * @uses $wp_query
376  *
377  * @return bool
378  */
379 function is_comment_feed() {
380         global $wp_query;
381
382         if ( ! isset( $wp_query ) ) {
383                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
384                 return false;
385         }
386
387         return $wp_query->is_comment_feed();
388 }
389
390 /**
391  * Is the query for the front page of the site?
392  *
393  * This is for what is displayed at your site's main URL.
394  *
395  * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
396  *
397  * If you set a static page for the front page of your site, this function will return
398  * true when viewing that page.
399  *
400  * Otherwise the same as @see is_home()
401  *
402  * @see WP_Query::is_front_page()
403  * @since 2.5.0
404  * @uses is_home()
405  * @uses get_option()
406  *
407  * @return bool True, if front of site.
408  */
409 function is_front_page() {
410         global $wp_query;
411
412         if ( ! isset( $wp_query ) ) {
413                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
414                 return false;
415         }
416
417         return $wp_query->is_front_page();
418 }
419
420 /**
421  * Is the query for the blog homepage?
422  *
423  * This is the page which shows the time based blog content of your site.
424  *
425  * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
426  *
427  * If you set a static page for the front page of your site, this function will return
428  * true only on the page you set as the "Posts page".
429  *
430  * @see is_front_page()
431  *
432  * @see WP_Query::is_home()
433  * @since 1.5.0
434  * @uses $wp_query
435  *
436  * @return bool True if blog view homepage.
437  */
438 function is_home() {
439         global $wp_query;
440
441         if ( ! isset( $wp_query ) ) {
442                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
443                 return false;
444         }
445
446         return $wp_query->is_home();
447 }
448
449 /**
450  * Is the query for an existing month archive?
451  *
452  * @see WP_Query::is_month()
453  * @since 1.5.0
454  * @uses $wp_query
455  *
456  * @return bool
457  */
458 function is_month() {
459         global $wp_query;
460
461         if ( ! isset( $wp_query ) ) {
462                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
463                 return false;
464         }
465
466         return $wp_query->is_month();
467 }
468
469 /**
470  * Is the query for an existing single page?
471  *
472  * If the $page parameter is specified, this function will additionally
473  * check if the query is for one of the pages specified.
474  *
475  * @see is_single()
476  * @see is_singular()
477  *
478  * @see WP_Query::is_page()
479  * @since 1.5.0
480  * @uses $wp_query
481  *
482  * @param mixed $page Page ID, title, slug, or array of such.
483  * @return bool
484  */
485 function is_page( $page = '' ) {
486         global $wp_query;
487
488         if ( ! isset( $wp_query ) ) {
489                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
490                 return false;
491         }
492
493         return $wp_query->is_page( $page );
494 }
495
496 /**
497  * Is the query for paged result and not for the first page?
498  *
499  * @see WP_Query::is_paged()
500  * @since 1.5.0
501  * @uses $wp_query
502  *
503  * @return bool
504  */
505 function is_paged() {
506         global $wp_query;
507
508         if ( ! isset( $wp_query ) ) {
509                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
510                 return false;
511         }
512
513         return $wp_query->is_paged();
514 }
515
516 /**
517  * Is the query for a post or page preview?
518  *
519  * @see WP_Query::is_preview()
520  * @since 2.0.0
521  * @uses $wp_query
522  *
523  * @return bool
524  */
525 function is_preview() {
526         global $wp_query;
527
528         if ( ! isset( $wp_query ) ) {
529                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
530                 return false;
531         }
532
533         return $wp_query->is_preview();
534 }
535
536 /**
537  * Is the query for the robots file?
538  *
539  * @see WP_Query::is_robots()
540  * @since 2.1.0
541  * @uses $wp_query
542  *
543  * @return bool
544  */
545 function is_robots() {
546         global $wp_query;
547
548         if ( ! isset( $wp_query ) ) {
549                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
550                 return false;
551         }
552
553         return $wp_query->is_robots();
554 }
555
556 /**
557  * Is the query for a search?
558  *
559  * @see WP_Query::is_search()
560  * @since 1.5.0
561  * @uses $wp_query
562  *
563  * @return bool
564  */
565 function is_search() {
566         global $wp_query;
567
568         if ( ! isset( $wp_query ) ) {
569                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
570                 return false;
571         }
572
573         return $wp_query->is_search();
574 }
575
576 /**
577  * Is the query for an existing single post?
578  *
579  * Works for any post type, except attachments and pages
580  *
581  * If the $post parameter is specified, this function will additionally
582  * check if the query is for one of the Posts specified.
583  *
584  * @see is_page()
585  * @see is_singular()
586  *
587  * @see WP_Query::is_single()
588  * @since 1.5.0
589  * @uses $wp_query
590  *
591  * @param mixed $post Post ID, title, slug, or array of such.
592  * @return bool
593  */
594 function is_single( $post = '' ) {
595         global $wp_query;
596
597         if ( ! isset( $wp_query ) ) {
598                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
599                 return false;
600         }
601
602         return $wp_query->is_single( $post );
603 }
604
605 /**
606  * Is the query for an existing single post of any post type (post, attachment, page, ... )?
607  *
608  * If the $post_types parameter is specified, this function will additionally
609  * check if the query is for one of the Posts Types specified.
610  *
611  * @see is_page()
612  * @see is_single()
613  *
614  * @see WP_Query::is_singular()
615  * @since 1.5.0
616  * @uses $wp_query
617  *
618  * @param mixed $post_types Optional. Post Type or array of Post Types
619  * @return bool
620  */
621 function is_singular( $post_types = '' ) {
622         global $wp_query;
623
624         if ( ! isset( $wp_query ) ) {
625                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
626                 return false;
627         }
628
629         return $wp_query->is_singular( $post_types );
630 }
631
632 /**
633  * Is the query for a specific time?
634  *
635  * @see WP_Query::is_time()
636  * @since 1.5.0
637  * @uses $wp_query
638  *
639  * @return bool
640  */
641 function is_time() {
642         global $wp_query;
643
644         if ( ! isset( $wp_query ) ) {
645                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
646                 return false;
647         }
648
649         return $wp_query->is_time();
650 }
651
652 /**
653  * Is the query for a trackback endpoint call?
654  *
655  * @see WP_Query::is_trackback()
656  * @since 1.5.0
657  * @uses $wp_query
658  *
659  * @return bool
660  */
661 function is_trackback() {
662         global $wp_query;
663
664         if ( ! isset( $wp_query ) ) {
665                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
666                 return false;
667         }
668
669         return $wp_query->is_trackback();
670 }
671
672 /**
673  * Is the query for an existing year archive?
674  *
675  * @see WP_Query::is_year()
676  * @since 1.5.0
677  * @uses $wp_query
678  *
679  * @return bool
680  */
681 function is_year() {
682         global $wp_query;
683
684         if ( ! isset( $wp_query ) ) {
685                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
686                 return false;
687         }
688
689         return $wp_query->is_year();
690 }
691
692 /**
693  * Is the query a 404 (returns no results)?
694  *
695  * @see WP_Query::is_404()
696  * @since 1.5.0
697  * @uses $wp_query
698  *
699  * @return bool
700  */
701 function is_404() {
702         global $wp_query;
703
704         if ( ! isset( $wp_query ) ) {
705                 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
706                 return false;
707         }
708
709         return $wp_query->is_404();
710 }
711
712 /**
713  * Is the query the main query?
714  *
715  * @since 3.3.0
716  *
717  * @return bool
718  */
719 function is_main_query() {
720         if ( 'pre_get_posts' === current_filter() ) {
721                 $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
722                         'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
723                 _doing_it_wrong( __FUNCTION__, $message, '3.7' );
724         }
725
726         global $wp_query;
727         return $wp_query->is_main_query();
728 }
729
730 /*
731  * The Loop. Post loop control.
732  */
733
734 /**
735  * Whether current WordPress query has results to loop over.
736  *
737  * @see WP_Query::have_posts()
738  * @since 1.5.0
739  * @uses $wp_query
740  *
741  * @return bool
742  */
743 function have_posts() {
744         global $wp_query;
745
746         return $wp_query->have_posts();
747 }
748
749 /**
750  * Whether the caller is in the Loop.
751  *
752  * @since 2.0.0
753  * @uses $wp_query
754  *
755  * @return bool True if caller is within loop, false if loop hasn't started or ended.
756  */
757 function in_the_loop() {
758         global $wp_query;
759
760         return $wp_query->in_the_loop;
761 }
762
763 /**
764  * Rewind the loop posts.
765  *
766  * @see WP_Query::rewind_posts()
767  * @since 1.5.0
768  * @uses $wp_query
769  *
770  * @return null
771  */
772 function rewind_posts() {
773         global $wp_query;
774
775         return $wp_query->rewind_posts();
776 }
777
778 /**
779  * Iterate the post index in the loop.
780  *
781  * @see WP_Query::the_post()
782  * @since 1.5.0
783  * @uses $wp_query
784  */
785 function the_post() {
786         global $wp_query;
787
788         $wp_query->the_post();
789 }
790
791 /*
792  * Comments loop.
793  */
794
795 /**
796  * Whether there are comments to loop over.
797  *
798  * @see WP_Query::have_comments()
799  * @since 2.2.0
800  * @uses $wp_query
801  *
802  * @return bool
803  */
804 function have_comments() {
805         global $wp_query;
806         return $wp_query->have_comments();
807 }
808
809 /**
810  * Iterate comment index in the comment loop.
811  *
812  * @see WP_Query::the_comment()
813  * @since 2.2.0
814  * @uses $wp_query
815  *
816  * @return object
817  */
818 function the_comment() {
819         global $wp_query;
820         return $wp_query->the_comment();
821 }
822
823 /*
824  * WP_Query
825  */
826
827 /**
828  * The WordPress Query class.
829  *
830  * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
831  *
832  * @since 1.5.0
833  */
834 class WP_Query {
835
836         /**
837          * Query vars set by the user
838          *
839          * @since 1.5.0
840          * @access public
841          * @var array
842          */
843         var $query;
844
845         /**
846          * Query vars, after parsing
847          *
848          * @since 1.5.0
849          * @access public
850          * @var array
851          */
852         var $query_vars = array();
853
854         /**
855          * Taxonomy query, as passed to get_tax_sql()
856          *
857          * @since 3.1.0
858          * @access public
859          * @var object WP_Tax_Query
860          */
861         var $tax_query;
862
863         /**
864          * Metadata query container
865          *
866          * @since 3.2.0
867          * @access public
868          * @var object WP_Meta_Query
869          */
870         var $meta_query = false;
871
872         /**
873          * Date query container
874          *
875          * @since 3.7.0
876          * @access public
877          * @var object WP_Date_Query
878          */
879         var $date_query = false;
880
881         /**
882          * Holds the data for a single object that is queried.
883          *
884          * Holds the contents of a post, page, category, attachment.
885          *
886          * @since 1.5.0
887          * @access public
888          * @var object|array
889          */
890         var $queried_object;
891
892         /**
893          * The ID of the queried object.
894          *
895          * @since 1.5.0
896          * @access public
897          * @var int
898          */
899         var $queried_object_id;
900
901         /**
902          * Get post database query.
903          *
904          * @since 2.0.1
905          * @access public
906          * @var string
907          */
908         var $request;
909
910         /**
911          * List of posts.
912          *
913          * @since 1.5.0
914          * @access public
915          * @var array
916          */
917         var $posts;
918
919         /**
920          * The amount of posts for the current query.
921          *
922          * @since 1.5.0
923          * @access public
924          * @var int
925          */
926         var $post_count = 0;
927
928         /**
929          * Index of the current item in the loop.
930          *
931          * @since 1.5.0
932          * @access public
933          * @var int
934          */
935         var $current_post = -1;
936
937         /**
938          * Whether the loop has started and the caller is in the loop.
939          *
940          * @since 2.0.0
941          * @access public
942          * @var bool
943          */
944         var $in_the_loop = false;
945
946         /**
947          * The current post.
948          *
949          * @since 1.5.0
950          * @access public
951          * @var WP_Post
952          */
953         var $post;
954
955         /**
956          * The list of comments for current post.
957          *
958          * @since 2.2.0
959          * @access public
960          * @var array
961          */
962         var $comments;
963
964         /**
965          * The amount of comments for the posts.
966          *
967          * @since 2.2.0
968          * @access public
969          * @var int
970          */
971         var $comment_count = 0;
972
973         /**
974          * The index of the comment in the comment loop.
975          *
976          * @since 2.2.0
977          * @access public
978          * @var int
979          */
980         var $current_comment = -1;
981
982         /**
983          * Current comment ID.
984          *
985          * @since 2.2.0
986          * @access public
987          * @var int
988          */
989         var $comment;
990
991         /**
992          * The amount of found posts for the current query.
993          *
994          * If limit clause was not used, equals $post_count.
995          *
996          * @since 2.1.0
997          * @access public
998          * @var int
999          */
1000         var $found_posts = 0;
1001
1002         /**
1003          * The amount of pages.
1004          *
1005          * @since 2.1.0
1006          * @access public
1007          * @var int
1008          */
1009         var $max_num_pages = 0;
1010
1011         /**
1012          * The amount of comment pages.
1013          *
1014          * @since 2.7.0
1015          * @access public
1016          * @var int
1017          */
1018         var $max_num_comment_pages = 0;
1019
1020         /**
1021          * Set if query is single post.
1022          *
1023          * @since 1.5.0
1024          * @access public
1025          * @var bool
1026          */
1027         var $is_single = false;
1028
1029         /**
1030          * Set if query is preview of blog.
1031          *
1032          * @since 2.0.0
1033          * @access public
1034          * @var bool
1035          */
1036         var $is_preview = false;
1037
1038         /**
1039          * Set if query returns a page.
1040          *
1041          * @since 1.5.0
1042          * @access public
1043          * @var bool
1044          */
1045         var $is_page = false;
1046
1047         /**
1048          * Set if query is an archive list.
1049          *
1050          * @since 1.5.0
1051          * @access public
1052          * @var bool
1053          */
1054         var $is_archive = false;
1055
1056         /**
1057          * Set if query is part of a date.
1058          *
1059          * @since 1.5.0
1060          * @access public
1061          * @var bool
1062          */
1063         var $is_date = false;
1064
1065         /**
1066          * Set if query contains a year.
1067          *
1068          * @since 1.5.0
1069          * @access public
1070          * @var bool
1071          */
1072         var $is_year = false;
1073
1074         /**
1075          * Set if query contains a month.
1076          *
1077          * @since 1.5.0
1078          * @access public
1079          * @var bool
1080          */
1081         var $is_month = false;
1082
1083         /**
1084          * Set if query contains a day.
1085          *
1086          * @since 1.5.0
1087          * @access public
1088          * @var bool
1089          */
1090         var $is_day = false;
1091
1092         /**
1093          * Set if query contains time.
1094          *
1095          * @since 1.5.0
1096          * @access public
1097          * @var bool
1098          */
1099         var $is_time = false;
1100
1101         /**
1102          * Set if query contains an author.
1103          *
1104          * @since 1.5.0
1105          * @access public
1106          * @var bool
1107          */
1108         var $is_author = false;
1109
1110         /**
1111          * Set if query contains category.
1112          *
1113          * @since 1.5.0
1114          * @access public
1115          * @var bool
1116          */
1117         var $is_category = false;
1118
1119         /**
1120          * Set if query contains tag.
1121          *
1122          * @since 2.3.0
1123          * @access public
1124          * @var bool
1125          */
1126         var $is_tag = false;
1127
1128         /**
1129          * Set if query contains taxonomy.
1130          *
1131          * @since 2.5.0
1132          * @access public
1133          * @var bool
1134          */
1135         var $is_tax = false;
1136
1137         /**
1138          * Set if query was part of a search result.
1139          *
1140          * @since 1.5.0
1141          * @access public
1142          * @var bool
1143          */
1144         var $is_search = false;
1145
1146         /**
1147          * Set if query is feed display.
1148          *
1149          * @since 1.5.0
1150          * @access public
1151          * @var bool
1152          */
1153         var $is_feed = false;
1154
1155         /**
1156          * Set if query is comment feed display.
1157          *
1158          * @since 2.2.0
1159          * @access public
1160          * @var bool
1161          */
1162         var $is_comment_feed = false;
1163
1164         /**
1165          * Set if query is trackback.
1166          *
1167          * @since 1.5.0
1168          * @access public
1169          * @var bool
1170          */
1171         var $is_trackback = false;
1172
1173         /**
1174          * Set if query is blog homepage.
1175          *
1176          * @since 1.5.0
1177          * @access public
1178          * @var bool
1179          */
1180         var $is_home = false;
1181
1182         /**
1183          * Set if query couldn't found anything.
1184          *
1185          * @since 1.5.0
1186          * @access public
1187          * @var bool
1188          */
1189         var $is_404 = false;
1190
1191         /**
1192          * Set if query is within comments popup window.
1193          *
1194          * @since 1.5.0
1195          * @access public
1196          * @var bool
1197          */
1198         var $is_comments_popup = false;
1199
1200         /**
1201          * Set if query is paged
1202          *
1203          * @since 1.5.0
1204          * @access public
1205          * @var bool
1206          */
1207         var $is_paged = false;
1208
1209         /**
1210          * Set if query is part of administration page.
1211          *
1212          * @since 1.5.0
1213          * @access public
1214          * @var bool
1215          */
1216         var $is_admin = false;
1217
1218         /**
1219          * Set if query is an attachment.
1220          *
1221          * @since 2.0.0
1222          * @access public
1223          * @var bool
1224          */
1225         var $is_attachment = false;
1226
1227         /**
1228          * Set if is single, is a page, or is an attachment.
1229          *
1230          * @since 2.1.0
1231          * @access public
1232          * @var bool
1233          */
1234         var $is_singular = false;
1235
1236         /**
1237          * Set if query is for robots.
1238          *
1239          * @since 2.1.0
1240          * @access public
1241          * @var bool
1242          */
1243         var $is_robots = false;
1244
1245         /**
1246          * Set if query contains posts.
1247          *
1248          * Basically, the homepage if the option isn't set for the static homepage.
1249          *
1250          * @since 2.1.0
1251          * @access public
1252          * @var bool
1253          */
1254         var $is_posts_page = false;
1255
1256         /**
1257          * Set if query is for a post type archive.
1258          *
1259          * @since 3.1.0
1260          * @access public
1261          * @var bool
1262          */
1263         var $is_post_type_archive = false;
1264
1265         /**
1266          * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1267          * whether we have to re-parse because something has changed
1268          *
1269          * @since 3.1.0
1270          * @access private
1271          */
1272         var $query_vars_hash = false;
1273
1274         /**
1275          * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1276          * via pre_get_posts hooks.
1277          *
1278          * @since 3.1.1
1279          * @access private
1280          */
1281         var $query_vars_changed = true;
1282
1283         /**
1284          * Set if post thumbnails are cached
1285          *
1286          * @since 3.2.0
1287          * @access public
1288          * @var bool
1289          */
1290          var $thumbnails_cached = false;
1291
1292         /**
1293          * Cached list of search stopwords.
1294          *
1295          * @since 3.7.0
1296          * @var array
1297          */
1298         private $stopwords;
1299
1300         /**
1301          * Resets query flags to false.
1302          *
1303          * The query flags are what page info WordPress was able to figure out.
1304          *
1305          * @since 2.0.0
1306          * @access private
1307          */
1308         function init_query_flags() {
1309                 $this->is_single = false;
1310                 $this->is_preview = false;
1311                 $this->is_page = false;
1312                 $this->is_archive = false;
1313                 $this->is_date = false;
1314                 $this->is_year = false;
1315                 $this->is_month = false;
1316                 $this->is_day = false;
1317                 $this->is_time = false;
1318                 $this->is_author = false;
1319                 $this->is_category = false;
1320                 $this->is_tag = false;
1321                 $this->is_tax = false;
1322                 $this->is_search = false;
1323                 $this->is_feed = false;
1324                 $this->is_comment_feed = false;
1325                 $this->is_trackback = false;
1326                 $this->is_home = false;
1327                 $this->is_404 = false;
1328                 $this->is_comments_popup = false;
1329                 $this->is_paged = false;
1330                 $this->is_admin = false;
1331                 $this->is_attachment = false;
1332                 $this->is_singular = false;
1333                 $this->is_robots = false;
1334                 $this->is_posts_page = false;
1335                 $this->is_post_type_archive = false;
1336         }
1337
1338         /**
1339          * Initiates object properties and sets default values.
1340          *
1341          * @since 1.5.0
1342          * @access public
1343          */
1344         function init() {
1345                 unset($this->posts);
1346                 unset($this->query);
1347                 $this->query_vars = array();
1348                 unset($this->queried_object);
1349                 unset($this->queried_object_id);
1350                 $this->post_count = 0;
1351                 $this->current_post = -1;
1352                 $this->in_the_loop = false;
1353                 unset( $this->request );
1354                 unset( $this->post );
1355                 unset( $this->comments );
1356                 unset( $this->comment );
1357                 $this->comment_count = 0;
1358                 $this->current_comment = -1;
1359                 $this->found_posts = 0;
1360                 $this->max_num_pages = 0;
1361                 $this->max_num_comment_pages = 0;
1362
1363                 $this->init_query_flags();
1364         }
1365
1366         /**
1367          * Reparse the query vars.
1368          *
1369          * @since 1.5.0
1370          * @access public
1371          */
1372         function parse_query_vars() {
1373                 $this->parse_query();
1374         }
1375
1376         /**
1377          * Fills in the query variables, which do not exist within the parameter.
1378          *
1379          * @since 2.1.0
1380          * @access public
1381          *
1382          * @param array $array Defined query variables.
1383          * @return array Complete query variables with undefined ones filled in empty.
1384          */
1385         function fill_query_vars($array) {
1386                 $keys = array(
1387                         'error'
1388                         , 'm'
1389                         , 'p'
1390                         , 'post_parent'
1391                         , 'subpost'
1392                         , 'subpost_id'
1393                         , 'attachment'
1394                         , 'attachment_id'
1395                         , 'name'
1396                         , 'static'
1397                         , 'pagename'
1398                         , 'page_id'
1399                         , 'second'
1400                         , 'minute'
1401                         , 'hour'
1402                         , 'day'
1403                         , 'monthnum'
1404                         , 'year'
1405                         , 'w'
1406                         , 'category_name'
1407                         , 'tag'
1408                         , 'cat'
1409                         , 'tag_id'
1410                         , 'author'
1411                         , 'author_name'
1412                         , 'feed'
1413                         , 'tb'
1414                         , 'paged'
1415                         , 'comments_popup'
1416                         , 'meta_key'
1417                         , 'meta_value'
1418                         , 'preview'
1419                         , 's'
1420                         , 'sentence'
1421                         , 'fields'
1422                         , 'menu_order'
1423                 );
1424
1425                 foreach ( $keys as $key ) {
1426                         if ( !isset($array[$key]) )
1427                                 $array[$key] = '';
1428                 }
1429
1430                 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1431                         'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
1432                         'author__in', 'author__not_in' );
1433
1434                 foreach ( $array_keys as $key ) {
1435                         if ( !isset($array[$key]) )
1436                                 $array[$key] = array();
1437                 }
1438                 return $array;
1439         }
1440
1441         /**
1442          * Parse a query string and set query type booleans.
1443          *
1444          * @since 1.5.0
1445          * @access public
1446          *
1447          * @param string|array $query Optional query.
1448          */
1449         function parse_query( $query =  '' ) {
1450                 if ( ! empty( $query ) ) {
1451                         $this->init();
1452                         $this->query = $this->query_vars = wp_parse_args( $query );
1453                 } elseif ( ! isset( $this->query ) ) {
1454                         $this->query = $this->query_vars;
1455                 }
1456
1457                 $this->query_vars = $this->fill_query_vars($this->query_vars);
1458                 $qv = &$this->query_vars;
1459                 $this->query_vars_changed = true;
1460
1461                 if ( ! empty($qv['robots']) )
1462                         $this->is_robots = true;
1463
1464                 $qv['p'] =  absint($qv['p']);
1465                 $qv['page_id'] =  absint($qv['page_id']);
1466                 $qv['year'] = absint($qv['year']);
1467                 $qv['monthnum'] = absint($qv['monthnum']);
1468                 $qv['day'] = absint($qv['day']);
1469                 $qv['w'] = absint($qv['w']);
1470                 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
1471                 $qv['paged'] = absint($qv['paged']);
1472                 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1473                 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
1474                 $qv['pagename'] = trim( $qv['pagename'] );
1475                 $qv['name'] = trim( $qv['name'] );
1476                 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1477                 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1478                 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1479                 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1480
1481                 // Fairly insane upper bound for search string lengths.
1482                 if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
1483                         $qv['s'] = '';
1484
1485                 // Compat. Map subpost to attachment.
1486                 if ( '' != $qv['subpost'] )
1487                         $qv['attachment'] = $qv['subpost'];
1488                 if ( '' != $qv['subpost_id'] )
1489                         $qv['attachment_id'] = $qv['subpost_id'];
1490
1491                 $qv['attachment_id'] = absint($qv['attachment_id']);
1492
1493                 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1494                         $this->is_single = true;
1495                         $this->is_attachment = true;
1496                 } elseif ( '' != $qv['name'] ) {
1497                         $this->is_single = true;
1498                 } elseif ( $qv['p'] ) {
1499                         $this->is_single = true;
1500                 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1501                         // If year, month, day, hour, minute, and second are set, a single
1502                         // post is being queried.
1503                         $this->is_single = true;
1504                 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1505                         $this->is_page = true;
1506                         $this->is_single = false;
1507                 } else {
1508                 // Look for archive queries. Dates, categories, authors, search, post type archives.
1509
1510                         if ( !empty($qv['s']) ) {
1511                                 $this->is_search = true;
1512                         }
1513
1514                         if ( '' !== $qv['second'] ) {
1515                                 $this->is_time = true;
1516                                 $this->is_date = true;
1517                         }
1518
1519                         if ( '' !== $qv['minute'] ) {
1520                                 $this->is_time = true;
1521                                 $this->is_date = true;
1522                         }
1523
1524                         if ( '' !== $qv['hour'] ) {
1525                                 $this->is_time = true;
1526                                 $this->is_date = true;
1527                         }
1528
1529                         if ( $qv['day'] ) {
1530                                 if ( ! $this->is_date ) {
1531                                         $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
1532                                         if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
1533                                                 $qv['error'] = '404';
1534                                         } else {
1535                                                 $this->is_day = true;
1536                                                 $this->is_date = true;
1537                                         }
1538                                 }
1539                         }
1540
1541                         if ( $qv['monthnum'] ) {
1542                                 if ( ! $this->is_date ) {
1543                                         if ( 12 < $qv['monthnum'] ) {
1544                                                 $qv['error'] = '404';
1545                                         } else {
1546                                                 $this->is_month = true;
1547                                                 $this->is_date = true;
1548                                         }
1549                                 }
1550                         }
1551
1552                         if ( $qv['year'] ) {
1553                                 if ( ! $this->is_date ) {
1554                                         $this->is_year = true;
1555                                         $this->is_date = true;
1556                                 }
1557                         }
1558
1559                         if ( $qv['m'] ) {
1560                                 $this->is_date = true;
1561                                 if ( strlen($qv['m']) > 9 ) {
1562                                         $this->is_time = true;
1563                                 } else if ( strlen($qv['m']) > 7 ) {
1564                                         $this->is_day = true;
1565                                 } else if ( strlen($qv['m']) > 5 ) {
1566                                         $this->is_month = true;
1567                                 } else {
1568                                         $this->is_year = true;
1569                                 }
1570                         }
1571
1572                         if ( '' != $qv['w'] ) {
1573                                 $this->is_date = true;
1574                         }
1575
1576                         $this->query_vars_hash = false;
1577                         $this->parse_tax_query( $qv );
1578
1579                         foreach ( $this->tax_query->queries as $tax_query ) {
1580                                 if ( 'NOT IN' != $tax_query['operator'] ) {
1581                                         switch ( $tax_query['taxonomy'] ) {
1582                                                 case 'category':
1583                                                         $this->is_category = true;
1584                                                         break;
1585                                                 case 'post_tag':
1586                                                         $this->is_tag = true;
1587                                                         break;
1588                                                 default:
1589                                                         $this->is_tax = true;
1590                                         }
1591                                 }
1592                         }
1593                         unset( $tax_query );
1594
1595                         if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1596                                 $this->is_author = false;
1597                         } else {
1598                                 $this->is_author = true;
1599                         }
1600
1601                         if ( '' != $qv['author_name'] )
1602                                 $this->is_author = true;
1603
1604                         if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1605                                 $post_type_obj = get_post_type_object( $qv['post_type'] );
1606                                 if ( ! empty( $post_type_obj->has_archive ) )
1607                                         $this->is_post_type_archive = true;
1608                         }
1609
1610                         if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1611                                 $this->is_archive = true;
1612                 }
1613
1614                 if ( '' != $qv['feed'] )
1615                         $this->is_feed = true;
1616
1617                 if ( '' != $qv['tb'] )
1618                         $this->is_trackback = true;
1619
1620                 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1621                         $this->is_paged = true;
1622
1623                 if ( '' != $qv['comments_popup'] )
1624                         $this->is_comments_popup = true;
1625
1626                 // if we're previewing inside the write screen
1627                 if ( '' != $qv['preview'] )
1628                         $this->is_preview = true;
1629
1630                 if ( is_admin() )
1631                         $this->is_admin = true;
1632
1633                 if ( false !== strpos($qv['feed'], 'comments-') ) {
1634                         $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1635                         $qv['withcomments'] = 1;
1636                 }
1637
1638                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1639
1640                 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1641                         $this->is_comment_feed = true;
1642
1643                 if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
1644                         $this->is_home = true;
1645
1646                 // Correct is_* for page_on_front and page_for_posts
1647                 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1648                         $_query = wp_parse_args($this->query);
1649                         // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1650                         if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1651                                 unset($_query['pagename']);
1652                         if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1653                                 $this->is_page = true;
1654                                 $this->is_home = false;
1655                                 $qv['page_id'] = get_option('page_on_front');
1656                                 // Correct <!--nextpage--> for page_on_front
1657                                 if ( !empty($qv['paged']) ) {
1658                                         $qv['page'] = $qv['paged'];
1659                                         unset($qv['paged']);
1660                                 }
1661                         }
1662                 }
1663
1664                 if ( '' != $qv['pagename'] ) {
1665                         $this->queried_object = get_page_by_path($qv['pagename']);
1666                         if ( !empty($this->queried_object) )
1667                                 $this->queried_object_id = (int) $this->queried_object->ID;
1668                         else
1669                                 unset($this->queried_object);
1670
1671                         if  ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1672                                 $this->is_page = false;
1673                                 $this->is_home = true;
1674                                 $this->is_posts_page = true;
1675                         }
1676                 }
1677
1678                 if ( $qv['page_id'] ) {
1679                         if  ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1680                                 $this->is_page = false;
1681                                 $this->is_home = true;
1682                                 $this->is_posts_page = true;
1683                         }
1684                 }
1685
1686                 if ( !empty($qv['post_type']) ) {
1687                         if ( is_array($qv['post_type']) )
1688                                 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1689                         else
1690                                 $qv['post_type'] = sanitize_key($qv['post_type']);
1691                 }
1692
1693                 if ( ! empty( $qv['post_status'] ) ) {
1694                         if ( is_array( $qv['post_status'] ) )
1695                                 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1696                         else
1697                                 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1698                 }
1699
1700                 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1701                         $this->is_comment_feed = false;
1702
1703                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1704                 // Done correcting is_* for page_on_front and page_for_posts
1705
1706                 if ( '404' == $qv['error'] )
1707                         $this->set_404();
1708
1709                 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1710                 $this->query_vars_changed = false;
1711
1712                 do_action_ref_array('parse_query', array(&$this));
1713         }
1714
1715         /**
1716          * Parses various taxonomy related query vars.
1717          *
1718          * @access protected
1719          * @since 3.1.0
1720          *
1721          * @param array &$q The query variables
1722          */
1723         function parse_tax_query( &$q ) {
1724                 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1725                         $tax_query = $q['tax_query'];
1726                 } else {
1727                         $tax_query = array();
1728                 }
1729
1730                 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1731                         $tax_query[] = array(
1732                                 'taxonomy' => $q['taxonomy'],
1733                                 'terms' => array( $q['term'] ),
1734                                 'field' => 'slug',
1735                         );
1736                 }
1737
1738                 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
1739                         if ( 'post_tag' == $taxonomy )
1740                                 continue;       // Handled further down in the $q['tag'] block
1741
1742                         if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1743                                 $tax_query_defaults = array(
1744                                         'taxonomy' => $taxonomy,
1745                                         'field' => 'slug',
1746                                 );
1747
1748                                 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1749                                         $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1750                                 }
1751
1752                                 $term = $q[$t->query_var];
1753
1754                                 if ( strpos($term, '+') !== false ) {
1755                                         $terms = preg_split( '/[+]+/', $term );
1756                                         foreach ( $terms as $term ) {
1757                                                 $tax_query[] = array_merge( $tax_query_defaults, array(
1758                                                         'terms' => array( $term )
1759                                                 ) );
1760                                         }
1761                                 } else {
1762                                         $tax_query[] = array_merge( $tax_query_defaults, array(
1763                                                 'terms' => preg_split( '/[,]+/', $term )
1764                                         ) );
1765                                 }
1766                         }
1767                 }
1768
1769                 // Category stuff
1770                 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
1771                         $cat_in = $cat_not_in = array();
1772
1773                         $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
1774                         $cat_array = array_map( 'intval', $cat_array );
1775                         $q['cat'] = implode( ',', $cat_array );
1776
1777                         foreach ( $cat_array as $cat ) {
1778                                 if ( $cat > 0 )
1779                                         $cat_in[] = $cat;
1780                                 elseif ( $cat < 0 )
1781                                         $cat_not_in[] = abs( $cat );
1782                         }
1783
1784                         if ( ! empty( $cat_in ) ) {
1785                                 $tax_query[] = array(
1786                                         'taxonomy' => 'category',
1787                                         'terms' => $cat_in,
1788                                         'field' => 'term_id',
1789                                         'include_children' => true
1790                                 );
1791                         }
1792
1793                         if ( ! empty( $cat_not_in ) ) {
1794                                 $tax_query[] = array(
1795                                         'taxonomy' => 'category',
1796                                         'terms' => $cat_not_in,
1797                                         'field' => 'term_id',
1798                                         'operator' => 'NOT IN',
1799                                         'include_children' => true
1800                                 );
1801                         }
1802                         unset( $cat_array, $cat_in, $cat_not_in );
1803                 }
1804
1805                 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1806                         $q['category__and'] = (array) $q['category__and'];
1807                         if ( ! isset( $q['category__in'] ) )
1808                                 $q['category__in'] = array();
1809                         $q['category__in'][] = absint( reset( $q['category__and'] ) );
1810                         unset( $q['category__and'] );
1811                 }
1812
1813                 if ( ! empty( $q['category__in'] ) ) {
1814                         $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1815                         $tax_query[] = array(
1816                                 'taxonomy' => 'category',
1817                                 'terms' => $q['category__in'],
1818                                 'field' => 'term_id',
1819                                 'include_children' => false
1820                         );
1821                 }
1822
1823                 if ( ! empty($q['category__not_in']) ) {
1824                         $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1825                         $tax_query[] = array(
1826                                 'taxonomy' => 'category',
1827                                 'terms' => $q['category__not_in'],
1828                                 'operator' => 'NOT IN',
1829                                 'include_children' => false
1830                         );
1831                 }
1832
1833                 if ( ! empty($q['category__and']) ) {
1834                         $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1835                         $tax_query[] = array(
1836                                 'taxonomy' => 'category',
1837                                 'terms' => $q['category__and'],
1838                                 'field' => 'term_id',
1839                                 'operator' => 'AND',
1840                                 'include_children' => false
1841                         );
1842                 }
1843
1844                 // Tag stuff
1845                 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1846                         if ( strpos($q['tag'], ',') !== false ) {
1847                                 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1848                                 foreach ( (array) $tags as $tag ) {
1849                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1850                                         $q['tag_slug__in'][] = $tag;
1851                                 }
1852                         } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1853                                 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1854                                 foreach ( (array) $tags as $tag ) {
1855                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1856                                         $q['tag_slug__and'][] = $tag;
1857                                 }
1858                         } else {
1859                                 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1860                                 $q['tag_slug__in'][] = $q['tag'];
1861                         }
1862                 }
1863
1864                 if ( !empty($q['tag_id']) ) {
1865                         $q['tag_id'] = absint( $q['tag_id'] );
1866                         $tax_query[] = array(
1867                                 'taxonomy' => 'post_tag',
1868                                 'terms' => $q['tag_id']
1869                         );
1870                 }
1871
1872                 if ( !empty($q['tag__in']) ) {
1873                         $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1874                         $tax_query[] = array(
1875                                 'taxonomy' => 'post_tag',
1876                                 'terms' => $q['tag__in']
1877                         );
1878                 }
1879
1880                 if ( !empty($q['tag__not_in']) ) {
1881                         $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1882                         $tax_query[] = array(
1883                                 'taxonomy' => 'post_tag',
1884                                 'terms' => $q['tag__not_in'],
1885                                 'operator' => 'NOT IN'
1886                         );
1887                 }
1888
1889                 if ( !empty($q['tag__and']) ) {
1890                         $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1891                         $tax_query[] = array(
1892                                 'taxonomy' => 'post_tag',
1893                                 'terms' => $q['tag__and'],
1894                                 'operator' => 'AND'
1895                         );
1896                 }
1897
1898                 if ( !empty($q['tag_slug__in']) ) {
1899                         $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1900                         $tax_query[] = array(
1901                                 'taxonomy' => 'post_tag',
1902                                 'terms' => $q['tag_slug__in'],
1903                                 'field' => 'slug'
1904                         );
1905                 }
1906
1907                 if ( !empty($q['tag_slug__and']) ) {
1908                         $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1909                         $tax_query[] = array(
1910                                 'taxonomy' => 'post_tag',
1911                                 'terms' => $q['tag_slug__and'],
1912                                 'field' => 'slug',
1913                                 'operator' => 'AND'
1914                         );
1915                 }
1916
1917                 $this->tax_query = new WP_Tax_Query( $tax_query );
1918
1919                 do_action( 'parse_tax_query', $this );
1920         }
1921
1922         /**
1923          * Generate SQL for the WHERE clause based on passed search terms.
1924          *
1925          * @since 3.7.0
1926          *
1927          * @global wpdb $wpdb
1928          * @param array $q Query variables.
1929          * @return string WHERE clause.
1930          */
1931         protected function parse_search( &$q ) {
1932                 global $wpdb;
1933
1934                 $search = '';
1935
1936                 // added slashes screw with quote grouping when done early, so done later
1937                 $q['s'] = stripslashes( $q['s'] );
1938                 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1939                         $q['s'] = urldecode( $q['s'] );
1940                 // there are no line breaks in <input /> fields
1941                 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1942                 $q['search_terms_count'] = 1;
1943                 if ( ! empty( $q['sentence'] ) ) {
1944                         $q['search_terms'] = array( $q['s'] );
1945                 } else {
1946                         if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1947                                 $q['search_terms_count'] = count( $matches[0] );
1948                                 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1949                                 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1950                                 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1951                                         $q['search_terms'] = array( $q['s'] );
1952                         } else {
1953                                 $q['search_terms'] = array( $q['s'] );
1954                         }
1955                 }
1956
1957                 $n = ! empty( $q['exact'] ) ? '' : '%';
1958                 $searchand = '';
1959                 $q['search_orderby_title'] = array();
1960                 foreach ( $q['search_terms'] as $term ) {
1961                         $term = like_escape( esc_sql( $term ) );
1962                         if ( $n )
1963                                 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1964
1965                         $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1966                         $searchand = ' AND ';
1967                 }
1968
1969                 if ( ! empty( $search ) ) {
1970                         $search = " AND ({$search}) ";
1971                         if ( ! is_user_logged_in() )
1972                                 $search .= " AND ($wpdb->posts.post_password = '') ";
1973                 }
1974
1975                 return $search;
1976         }
1977
1978         /**
1979          * Check if the terms are suitable for searching.
1980          *
1981          * Uses an array of stopwords (terms) that are excluded from the separate
1982          * term matching when searching for posts. The list of English stopwords is
1983          * the approximate search engines list, and is translatable.
1984          *
1985          * @since 3.7.0
1986          *
1987          * @param array Terms to check.
1988          * @return array Terms that are not stopwords.
1989          */
1990         protected function parse_search_terms( $terms ) {
1991                 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
1992                 $checked = array();
1993
1994                 $stopwords = $this->get_search_stopwords();
1995
1996                 foreach ( $terms as $term ) {
1997                         // keep before/after spaces when term is for exact match
1998                         if ( preg_match( '/^".+"$/', $term ) )
1999                                 $term = trim( $term, "\"'" );
2000                         else
2001                                 $term = trim( $term, "\"' " );
2002
2003                         // Avoid single A-Z.
2004                         if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
2005                                 continue;
2006
2007                         if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2008                                 continue;
2009
2010                         $checked[] = $term;
2011                 }
2012
2013                 return $checked;
2014         }
2015
2016         /**
2017          * Retrieve stopwords used when parsing search terms.
2018          *
2019          * @since 3.7.0
2020          *
2021          * @return array Stopwords.
2022          */
2023         protected function get_search_stopwords() {
2024                 if ( isset( $this->stopwords ) )
2025                         return $this->stopwords;
2026
2027                 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2028                  * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2029                  * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2030                  */
2031                 $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
2032                         'Comma-separated list of search stopwords in your language' ) );
2033
2034                 $stopwords = array();
2035                 foreach( $words as $word ) {
2036                         $word = trim( $word, "\r\n\t " );
2037                         if ( $word )
2038                                 $stopwords[] = $word;
2039                 }
2040
2041                 /**
2042                  * Filter stopwords used when parsing search terms.
2043                  *
2044                  * @since 3.7.0
2045                  *
2046                  * @param array $stopwords Stopwords.
2047                  */
2048                 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2049                 return $this->stopwords;
2050         }
2051
2052         /**
2053          * Generate SQL for the ORDER BY condition based on passed search terms.
2054          *
2055          * @global wpdb $wpdb
2056          * @param array $q Query variables.
2057          * @return string ORDER BY clause.
2058          */
2059         protected function parse_search_order( &$q ) {
2060                 global $wpdb;
2061
2062                 $search_orderby = '';
2063
2064                 if ( $q['search_terms_count'] > 1 ) {
2065                         $num_terms = count( $q['search_orderby_title'] );
2066                         $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2067
2068                         $search_orderby = '(CASE ';
2069                         // sentence match in 'post_title'
2070                         $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2071
2072                         // sanity limit, sort as sentence when more than 6 terms
2073                         // (few searches are longer than 6 terms and most titles are not)
2074                         if ( $num_terms < 7 ) {
2075                                 // all words in title
2076                                 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2077                                 // any word in title, not needed when $num_terms == 1
2078                                 if ( $num_terms > 1 )
2079                                         $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2080                         }
2081
2082                         // sentence match in 'post_content'
2083                         $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2084                         $search_orderby .= 'ELSE 5 END)';
2085                 } else {
2086                         // single word or sentence search
2087                         $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2088                 }
2089
2090                 return $search_orderby;
2091         }
2092
2093         /**
2094          * Sets the 404 property and saves whether query is feed.
2095          *
2096          * @since 2.0.0
2097          * @access public
2098          */
2099         function set_404() {
2100                 $is_feed = $this->is_feed;
2101
2102                 $this->init_query_flags();
2103                 $this->is_404 = true;
2104
2105                 $this->is_feed = $is_feed;
2106         }
2107
2108         /**
2109          * Retrieve query variable.
2110          *
2111          * @since 1.5.0
2112          * @access public
2113          *
2114          * @param string $query_var Query variable key.
2115          * @return mixed
2116          */
2117         function get($query_var) {
2118                 if ( isset($this->query_vars[$query_var]) )
2119                         return $this->query_vars[$query_var];
2120
2121                 return '';
2122         }
2123
2124         /**
2125          * Set query variable.
2126          *
2127          * @since 1.5.0
2128          * @access public
2129          *
2130          * @param string $query_var Query variable key.
2131          * @param mixed $value Query variable value.
2132          */
2133         function set($query_var, $value) {
2134                 $this->query_vars[$query_var] = $value;
2135         }
2136
2137         /**
2138          * Retrieve the posts based on query variables.
2139          *
2140          * There are a few filters and actions that can be used to modify the post
2141          * database query.
2142          *
2143          * @since 1.5.0
2144          * @access public
2145          * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2146          *
2147          * @return array List of posts.
2148          */
2149         function get_posts() {
2150                 global $wpdb;
2151
2152                 $this->parse_query();
2153
2154                 do_action_ref_array('pre_get_posts', array(&$this));
2155
2156                 // Shorthand.
2157                 $q = &$this->query_vars;
2158
2159                 // Fill again in case pre_get_posts unset some vars.
2160                 $q = $this->fill_query_vars($q);
2161
2162                 // Parse meta query
2163                 $this->meta_query = new WP_Meta_Query();
2164                 $this->meta_query->parse_query_vars( $q );
2165
2166                 // Set a flag if a pre_get_posts hook changed the query vars.
2167                 $hash = md5( serialize( $this->query_vars ) );
2168                 if ( $hash != $this->query_vars_hash ) {
2169                         $this->query_vars_changed = true;
2170                         $this->query_vars_hash = $hash;
2171                 }
2172                 unset($hash);
2173
2174                 // First let's clear some variables
2175                 $distinct = '';
2176                 $whichauthor = '';
2177                 $whichmimetype = '';
2178                 $where = '';
2179                 $limits = '';
2180                 $join = '';
2181                 $search = '';
2182                 $groupby = '';
2183                 $fields = '';
2184                 $post_status_join = false;
2185                 $page = 1;
2186
2187                 if ( isset( $q['caller_get_posts'] ) ) {
2188                         _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2189                         if ( !isset( $q['ignore_sticky_posts'] ) )
2190                                 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2191                 }
2192
2193                 if ( !isset( $q['ignore_sticky_posts'] ) )
2194                         $q['ignore_sticky_posts'] = false;
2195
2196                 if ( !isset($q['suppress_filters']) )
2197                         $q['suppress_filters'] = false;
2198
2199                 if ( !isset($q['cache_results']) ) {
2200                         if ( wp_using_ext_object_cache() )
2201                                 $q['cache_results'] = false;
2202                         else
2203                                 $q['cache_results'] = true;
2204                 }
2205
2206                 if ( !isset($q['update_post_term_cache']) )
2207                         $q['update_post_term_cache'] = true;
2208
2209                 if ( !isset($q['update_post_meta_cache']) )
2210                         $q['update_post_meta_cache'] = true;
2211
2212                 if ( !isset($q['post_type']) ) {
2213                         if ( $this->is_search )
2214                                 $q['post_type'] = 'any';
2215                         else
2216                                 $q['post_type'] = '';
2217                 }
2218                 $post_type = $q['post_type'];
2219                 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
2220                         $q['posts_per_page'] = get_option('posts_per_page');
2221                 if ( isset($q['showposts']) && $q['showposts'] ) {
2222                         $q['showposts'] = (int) $q['showposts'];
2223                         $q['posts_per_page'] = $q['showposts'];
2224                 }
2225                 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2226                         $q['posts_per_page'] = $q['posts_per_archive_page'];
2227                 if ( !isset($q['nopaging']) ) {
2228                         if ( $q['posts_per_page'] == -1 ) {
2229                                 $q['nopaging'] = true;
2230                         } else {
2231                                 $q['nopaging'] = false;
2232                         }
2233                 }
2234                 if ( $this->is_feed ) {
2235                         $q['posts_per_page'] = get_option('posts_per_rss');
2236                         $q['nopaging'] = false;
2237                 }
2238                 $q['posts_per_page'] = (int) $q['posts_per_page'];
2239                 if ( $q['posts_per_page'] < -1 )
2240                         $q['posts_per_page'] = abs($q['posts_per_page']);
2241                 else if ( $q['posts_per_page'] == 0 )
2242                         $q['posts_per_page'] = 1;
2243
2244                 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2245                         $q['comments_per_page'] = get_option('comments_per_page');
2246
2247                 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2248                         $this->is_page = true;
2249                         $this->is_home = false;
2250                         $q['page_id'] = get_option('page_on_front');
2251                 }
2252
2253                 if ( isset($q['page']) ) {
2254                         $q['page'] = trim($q['page'], '/');
2255                         $q['page'] = absint($q['page']);
2256                 }
2257
2258                 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2259                 if ( isset($q['no_found_rows']) )
2260                         $q['no_found_rows'] = (bool) $q['no_found_rows'];
2261                 else
2262                         $q['no_found_rows'] = false;
2263
2264                 switch ( $q['fields'] ) {
2265                         case 'ids':
2266                                 $fields = "$wpdb->posts.ID";
2267                                 break;
2268                         case 'id=>parent':
2269                                 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2270                                 break;
2271                         default:
2272                                 $fields = "$wpdb->posts.*";
2273                 }
2274
2275                 if ( '' !== $q['menu_order'] )
2276                         $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2277
2278                 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2279                 if ( $q['m'] ) {
2280                         $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2281                         if ( strlen($q['m']) > 5 )
2282                                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2283                         if ( strlen($q['m']) > 7 )
2284                                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2285                         if ( strlen($q['m']) > 9 )
2286                                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2287                         if ( strlen($q['m']) > 11 )
2288                                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2289                         if ( strlen($q['m']) > 13 )
2290                                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2291                 }
2292
2293                 // Handle the other individual date parameters
2294                 $date_parameters = array();
2295
2296                 if ( '' !== $q['hour'] )
2297                         $date_parameters['hour'] = $q['hour'];
2298
2299                 if ( '' !== $q['minute'] )
2300                         $date_parameters['minute'] = $q['minute'];
2301
2302                 if ( '' !== $q['second'] )
2303                         $date_parameters['second'] = $q['second'];
2304
2305                 if ( $q['year'] )
2306                         $date_parameters['year'] = $q['year'];
2307
2308                 if ( $q['monthnum'] )
2309                         $date_parameters['monthnum'] = $q['monthnum'];
2310
2311                 if ( $q['w'] )
2312                         $date_parameters['week'] = $q['w'];
2313
2314                 if ( $q['day'] )
2315                         $date_parameters['day'] = $q['day'];
2316
2317                 if ( $date_parameters ) {
2318                         $date_query = new WP_Date_Query( array( $date_parameters ) );
2319                         $where .= $date_query->get_sql();
2320                 }
2321                 unset( $date_parameters, $date_query );
2322
2323                 // Handle complex date queries
2324                 if ( ! empty( $q['date_query'] ) ) {
2325                         $this->date_query = new WP_Date_Query( $q['date_query'] );
2326                         $where .= $this->date_query->get_sql();
2327                 }
2328
2329
2330                 // If we've got a post_type AND it's not "any" post_type.
2331                 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2332                         foreach ( (array)$q['post_type'] as $_post_type ) {
2333                                 $ptype_obj = get_post_type_object($_post_type);
2334                                 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2335                                         continue;
2336
2337                                 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2338                                         // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2339                                         $q['name'] = $q[ $ptype_obj->query_var ];
2340                                 } else {
2341                                         // Hierarchical post_types will operate through the
2342                                         $q['pagename'] = $q[ $ptype_obj->query_var ];
2343                                         $q['name'] = '';
2344                                 }
2345
2346                                 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2347                                 break;
2348                         } //end foreach
2349                         unset($ptype_obj);
2350                 }
2351
2352                 if ( '' != $q['name'] ) {
2353                         $q['name'] = sanitize_title_for_query( $q['name'] );
2354                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2355                 } elseif ( '' != $q['pagename'] ) {
2356                         if ( isset($this->queried_object_id) ) {
2357                                 $reqpage = $this->queried_object_id;
2358                         } else {
2359                                 if ( 'page' != $q['post_type'] ) {
2360                                         foreach ( (array)$q['post_type'] as $_post_type ) {
2361                                                 $ptype_obj = get_post_type_object($_post_type);
2362                                                 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2363                                                         continue;
2364
2365                                                 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2366                                                 if ( $reqpage )
2367                                                         break;
2368                                         }
2369                                         unset($ptype_obj);
2370                                 } else {
2371                                         $reqpage = get_page_by_path($q['pagename']);
2372                                 }
2373                                 if ( !empty($reqpage) )
2374                                         $reqpage = $reqpage->ID;
2375                                 else
2376                                         $reqpage = 0;
2377                         }
2378
2379                         $page_for_posts = get_option('page_for_posts');
2380                         if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2381                                 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2382                                 $q['name'] = $q['pagename'];
2383                                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2384                                 $reqpage_obj = get_post( $reqpage );
2385                                 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2386                                         $this->is_attachment = true;
2387                                         $post_type = $q['post_type'] = 'attachment';
2388                                         $this->is_page = true;
2389                                         $q['attachment_id'] = $reqpage;
2390                                 }
2391                         }
2392                 } elseif ( '' != $q['attachment'] ) {
2393                         $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2394                         $q['name'] = $q['attachment'];
2395                         $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2396                 }
2397
2398
2399                 if ( intval($q['comments_popup']) )
2400                         $q['p'] = absint($q['comments_popup']);
2401
2402                 // If an attachment is requested by number, let it supersede any post number.
2403                 if ( $q['attachment_id'] )
2404                         $q['p'] = absint($q['attachment_id']);
2405
2406                 // If a post number is specified, load that post
2407                 if ( $q['p'] ) {
2408                         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2409                 } elseif ( $q['post__in'] ) {
2410                         $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2411                         $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2412                 } elseif ( $q['post__not_in'] ) {
2413                         $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
2414                         $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2415                 }
2416
2417                 if ( is_numeric( $q['post_parent'] ) ) {
2418                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2419                 } elseif ( $q['post_parent__in'] ) {
2420                         $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2421                         $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2422                 } elseif ( $q['post_parent__not_in'] ) {
2423                         $post_parent__not_in = implode( ',',  array_map( 'absint', $q['post_parent__not_in'] ) );
2424                         $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2425                 }
2426
2427                 if ( $q['page_id'] ) {
2428                         if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2429                                 $q['p'] = $q['page_id'];
2430                                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2431                         }
2432                 }
2433
2434                 // If a search pattern is specified, load the posts that match.
2435                 if ( ! empty( $q['s'] ) )
2436                         $search = $this->parse_search( $q );
2437
2438                 /**
2439                  * Filter the search SQL that is used in the WHERE clause of WP_Query.
2440                  *
2441                  * @since 3.0.0
2442                  *
2443                  * @param string   $search Search SQL for WHERE clause.
2444                  * @param WP_Query $this   The current WP_Query object.
2445                  */
2446                 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2447
2448                 // Taxonomies
2449                 if ( !$this->is_singular ) {
2450                         $this->parse_tax_query( $q );
2451
2452                         $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2453
2454                         $join .= $clauses['join'];
2455                         $where .= $clauses['where'];
2456                 }
2457
2458                 if ( $this->is_tax ) {
2459                         if ( empty($post_type) ) {
2460                                 // Do a fully inclusive search for currently registered post types of queried taxonomies
2461                                 $post_type = array();
2462                                 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2463                                 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2464                                         $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2465                                         if ( array_intersect( $taxonomies, $object_taxonomies ) )
2466                                                 $post_type[] = $pt;
2467                                 }
2468                                 if ( ! $post_type )
2469                                         $post_type = 'any';
2470                                 elseif ( count( $post_type ) == 1 )
2471                                         $post_type = $post_type[0];
2472
2473                                 $post_status_join = true;
2474                         } elseif ( in_array('attachment', (array) $post_type) ) {
2475                                 $post_status_join = true;
2476                         }
2477                 }
2478
2479                 // Back-compat
2480                 if ( !empty($this->tax_query->queries) ) {
2481                         $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2482                         if ( !empty( $tax_query_in_and ) ) {
2483                                 if ( !isset( $q['taxonomy'] ) ) {
2484                                         foreach ( $tax_query_in_and as $a_tax_query ) {
2485                                                 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2486                                                         $q['taxonomy'] = $a_tax_query['taxonomy'];
2487                                                         if ( 'slug' == $a_tax_query['field'] )
2488                                                                 $q['term'] = $a_tax_query['terms'][0];
2489                                                         else
2490                                                                 $q['term_id'] = $a_tax_query['terms'][0];
2491
2492                                                         break;
2493                                                 }
2494                                         }
2495                                 }
2496
2497                                 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2498                                 if ( ! empty( $cat_query ) ) {
2499                                         $cat_query = reset( $cat_query );
2500
2501                                         if ( ! empty( $cat_query['terms'][0] ) ) {
2502                                                 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2503                                                 if ( $the_cat ) {
2504                                                         $this->set( 'cat', $the_cat->term_id );
2505                                                         $this->set( 'category_name', $the_cat->slug );
2506                                                 }
2507                                                 unset( $the_cat );
2508                                         }
2509                                 }
2510                                 unset( $cat_query );
2511
2512                                 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2513                                 if ( ! empty( $tag_query ) ) {
2514                                         $tag_query = reset( $tag_query );
2515
2516                                         if ( ! empty( $tag_query['terms'][0] ) ) {
2517                                                 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2518                                                 if ( $the_tag )
2519                                                         $this->set( 'tag_id', $the_tag->term_id );
2520                                                 unset( $the_tag );
2521                                         }
2522                                 }
2523                                 unset( $tag_query );
2524                         }
2525                 }
2526
2527                 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2528                         $groupby = "{$wpdb->posts}.ID";
2529                 }
2530
2531                 // Author/user stuff
2532
2533                 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2534                         $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2535                         $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2536                         foreach ( $authors as $author ) {
2537                                 $key = $author > 0 ? 'author__in' : 'author__not_in';
2538                                 $q[$key][] = abs( $author );
2539                         }
2540                         $q['author'] = implode( ',', $authors );
2541                 }
2542
2543                 if ( ! empty( $q['author__not_in'] ) ) {
2544                         $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2545                         $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2546                 } elseif ( ! empty( $q['author__in'] ) ) {
2547                         $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2548                         $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2549                 }
2550
2551                 // Author stuff for nice URLs
2552
2553                 if ( '' != $q['author_name'] ) {
2554                         if ( strpos($q['author_name'], '/') !== false ) {
2555                                 $q['author_name'] = explode('/', $q['author_name']);
2556                                 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2557                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2558                                 } else {
2559                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2560                                 }
2561                         }
2562                         $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2563                         $q['author'] = get_user_by('slug', $q['author_name']);
2564                         if ( $q['author'] )
2565                                 $q['author'] = $q['author']->ID;
2566                         $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2567                 }
2568
2569                 // MIME-Type stuff for attachment browsing
2570
2571                 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2572                         $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2573
2574                 $where .= $search . $whichauthor . $whichmimetype;
2575
2576                 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2577                         $q['order'] = 'DESC';
2578
2579                 // Order by
2580                 if ( empty($q['orderby']) ) {
2581                         $orderby = "$wpdb->posts.post_date " . $q['order'];
2582                 } elseif ( 'none' == $q['orderby'] ) {
2583                         $orderby = '';
2584                 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2585                         $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2586                 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2587                         $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2588                 } else {
2589                         // Used to filter values
2590                         $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2591                         if ( !empty($q['meta_key']) ) {
2592                                 $allowed_keys[] = $q['meta_key'];
2593                                 $allowed_keys[] = 'meta_value';
2594                                 $allowed_keys[] = 'meta_value_num';
2595                         }
2596                         $q['orderby'] = urldecode($q['orderby']);
2597                         $q['orderby'] = addslashes_gpc($q['orderby']);
2598
2599                         $orderby_array = array();
2600                         foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2601                                 // Only allow certain values for safety
2602                                 if ( ! in_array($orderby, $allowed_keys) )
2603                                         continue;
2604
2605                                 switch ( $orderby ) {
2606                                         case 'menu_order':
2607                                                 $orderby = "$wpdb->posts.menu_order";
2608                                                 break;
2609                                         case 'ID':
2610                                                 $orderby = "$wpdb->posts.ID";
2611                                                 break;
2612                                         case 'rand':
2613                                                 $orderby = 'RAND()';
2614                                                 break;
2615                                         case $q['meta_key']:
2616                                         case 'meta_value':
2617                                                 if ( isset( $q['meta_type'] ) ) {
2618                                                         $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2619                                                         $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2620                                                 } else {
2621                                                         $orderby = "$wpdb->postmeta.meta_value";
2622                                                 }
2623                                                 break;
2624                                         case 'meta_value_num':
2625                                                 $orderby = "$wpdb->postmeta.meta_value+0";
2626                                                 break;
2627                                         case 'comment_count':
2628                                                 $orderby = "$wpdb->posts.comment_count";
2629                                                 break;
2630                                         default:
2631                                                 $orderby = "$wpdb->posts.post_" . $orderby;
2632                                 }
2633
2634                                 $orderby_array[] = $orderby;
2635                         }
2636                         $orderby = implode( ',', $orderby_array );
2637
2638                         if ( empty( $orderby ) )
2639                                 $orderby = "$wpdb->posts.post_date ".$q['order'];
2640                         else
2641                                 $orderby .= " {$q['order']}";
2642                 }
2643
2644                 // Order search results by relevance only when another "orderby" is not specified in the query.
2645                 if ( ! empty( $q['s'] ) ) {
2646                         $search_orderby = '';
2647                         if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2648                                 $search_orderby = $this->parse_search_order( $q );
2649
2650                         /**
2651                          * Filter the ORDER BY used when ordering search results.
2652                          *
2653                          * @since 3.7.0
2654                          *
2655                          * @param string   $search_orderby The ORDER BY clause.
2656                          * @param WP_Query $this           The current WP_Query instance.
2657                          */
2658                         $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2659                         if ( $search_orderby )
2660                                 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2661                 }
2662
2663                 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2664                         $post_type_cap = 'multiple_post_type';
2665                 } else {
2666                         if ( is_array( $post_type ) )
2667                                 $post_type = reset( $post_type );
2668                         $post_type_object = get_post_type_object( $post_type );
2669                         if ( empty( $post_type_object ) )
2670                                 $post_type_cap = $post_type;
2671                 }
2672
2673                 if ( 'any' == $post_type ) {
2674                         $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2675                         if ( empty( $in_search_post_types ) )
2676                                 $where .= ' AND 1=0 ';
2677                         else
2678                                 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2679                 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2680                         $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2681                 } elseif ( ! empty( $post_type ) ) {
2682                         $where .= " AND $wpdb->posts.post_type = '$post_type'";
2683                         $post_type_object = get_post_type_object ( $post_type );
2684                 } elseif ( $this->is_attachment ) {
2685                         $where .= " AND $wpdb->posts.post_type = 'attachment'";
2686                         $post_type_object = get_post_type_object ( 'attachment' );
2687                 } elseif ( $this->is_page ) {
2688                         $where .= " AND $wpdb->posts.post_type = 'page'";
2689                         $post_type_object = get_post_type_object ( 'page' );
2690                 } else {
2691                         $where .= " AND $wpdb->posts.post_type = 'post'";
2692                         $post_type_object = get_post_type_object ( 'post' );
2693                 }
2694
2695                 $edit_cap = 'edit_post';
2696                 $read_cap = 'read_post';
2697
2698                 if ( ! empty( $post_type_object ) ) {
2699                         $edit_others_cap = $post_type_object->cap->edit_others_posts;
2700                         $read_private_cap = $post_type_object->cap->read_private_posts;
2701                 } else {
2702                         $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2703                         $read_private_cap = 'read_private_' . $post_type_cap . 's';
2704                 }
2705
2706                 $user_id = get_current_user_id();
2707
2708                 if ( ! empty( $q['post_status'] ) ) {
2709                         $statuswheres = array();
2710                         $q_status = $q['post_status'];
2711                         if ( ! is_array( $q_status ) )
2712                                 $q_status = explode(',', $q_status);
2713                         $r_status = array();
2714                         $p_status = array();
2715                         $e_status = array();
2716                         if ( in_array('any', $q_status) ) {
2717                                 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2718                                         $e_status[] = "$wpdb->posts.post_status <> '$status'";
2719                         } else {
2720                                 foreach ( get_post_stati() as $status ) {
2721                                         if ( in_array( $status, $q_status ) ) {
2722                                                 if ( 'private' == $status )
2723                                                         $p_status[] = "$wpdb->posts.post_status = '$status'";
2724                                                 else
2725                                                         $r_status[] = "$wpdb->posts.post_status = '$status'";
2726                                         }
2727                                 }
2728                         }
2729
2730                         if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2731                                 $r_status = array_merge($r_status, $p_status);
2732                                 unset($p_status);
2733                         }
2734
2735                         if ( !empty($e_status) ) {
2736                                 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2737                         }
2738                         if ( !empty($r_status) ) {
2739                                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2740                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2741                                 else
2742                                         $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2743                         }
2744                         if ( !empty($p_status) ) {
2745                                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2746                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2747                                 else
2748                                         $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2749                         }
2750                         if ( $post_status_join ) {
2751                                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2752                                 foreach ( $statuswheres as $index => $statuswhere )
2753                                         $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2754                         }
2755                         foreach ( $statuswheres as $statuswhere )
2756                                 $where .= " AND $statuswhere";
2757                 } elseif ( !$this->is_singular ) {
2758                         $where .= " AND ($wpdb->posts.post_status = 'publish'";
2759
2760                         // Add public states.
2761                         $public_states = get_post_stati( array('public' => true) );
2762                         foreach ( (array) $public_states as $state ) {
2763                                 if ( 'publish' == $state ) // Publish is hard-coded above.
2764                                         continue;
2765                                 $where .= " OR $wpdb->posts.post_status = '$state'";
2766                         }
2767
2768                         if ( $this->is_admin ) {
2769                                 // Add protected states that should show in the admin all list.
2770                                 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2771                                 foreach ( (array) $admin_all_states as $state )
2772                                         $where .= " OR $wpdb->posts.post_status = '$state'";
2773                         }
2774
2775                         if ( is_user_logged_in() ) {
2776                                 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2777                                 $private_states = get_post_stati( array('private' => true) );
2778                                 foreach ( (array) $private_states as $state )
2779                                         $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
2780                         }
2781
2782                         $where .= ')';
2783                 }
2784
2785                 if ( !empty( $this->meta_query->queries ) ) {
2786                         $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2787                         $join .= $clauses['join'];
2788                         $where .= $clauses['where'];
2789                 }
2790
2791                 // Apply filters on where and join prior to paging so that any
2792                 // manipulations to them are reflected in the paging by day queries.
2793                 if ( !$q['suppress_filters'] ) {
2794                         $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2795                         $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2796                 }
2797
2798                 // Paging
2799                 if ( empty($q['nopaging']) && !$this->is_singular ) {
2800                         $page = absint($q['paged']);
2801                         if ( !$page )
2802                                 $page = 1;
2803
2804                         if ( empty($q['offset']) ) {
2805                                 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2806                         } else { // we're ignoring $page and using 'offset'
2807                                 $q['offset'] = absint($q['offset']);
2808                                 $pgstrt = $q['offset'] . ', ';
2809                         }
2810                         $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2811                 }
2812
2813                 // Comments feeds
2814                 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2815                         if ( $this->is_archive || $this->is_search ) {
2816                                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2817                                 $cwhere = "WHERE comment_approved = '1' $where";
2818                                 $cgroupby = "$wpdb->comments.comment_id";
2819                         } else { // Other non singular e.g. front
2820                                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2821                                 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2822                                 $cgroupby = '';
2823                         }
2824
2825                         if ( !$q['suppress_filters'] ) {
2826                                 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2827                                 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2828                                 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2829                                 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2830                                 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2831                         }
2832                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2833                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2834
2835                         $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2836                         $this->comment_count = count($this->comments);
2837
2838                         $post_ids = array();
2839
2840                         foreach ( $this->comments as $comment )
2841                                 $post_ids[] = (int) $comment->comment_post_ID;
2842
2843                         $post_ids = join(',', $post_ids);
2844                         $join = '';
2845                         if ( $post_ids )
2846                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2847                         else
2848                                 $where = "AND 0";
2849                 }
2850
2851                 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2852
2853                 // Apply post-paging filters on where and join. Only plugins that
2854                 // manipulate paging queries should use these hooks.
2855                 if ( !$q['suppress_filters'] ) {
2856                         $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2857                         $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ) );
2858                         $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
2859                         $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ) );
2860                         $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
2861                         $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) );
2862                         $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) );
2863
2864                         // Filter all clauses at once, for convenience
2865                         $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2866                         foreach ( $pieces as $piece )
2867                                 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2868                 }
2869
2870                 // Announce current selection parameters. For use by caching plugins.
2871                 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2872
2873                 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2874                 if ( !$q['suppress_filters'] ) {
2875                         $where          = apply_filters_ref_array( 'posts_where_request',               array( $where, &$this ) );
2876                         $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ) );
2877                         $join           = apply_filters_ref_array( 'posts_join_request',                array( $join, &$this ) );
2878                         $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ) );
2879                         $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
2880                         $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) );
2881                         $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) );
2882
2883                         // Filter all clauses at once, for convenience
2884                         $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2885                         foreach ( $pieces as $piece )
2886                                 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2887                 }
2888
2889                 if ( ! empty($groupby) )
2890                         $groupby = 'GROUP BY ' . $groupby;
2891                 if ( !empty( $orderby ) )
2892                         $orderby = 'ORDER BY ' . $orderby;
2893
2894                 $found_rows = '';
2895                 if ( !$q['no_found_rows'] && !empty($limits) )
2896                         $found_rows = 'SQL_CALC_FOUND_ROWS';
2897
2898                 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2899
2900                 if ( !$q['suppress_filters'] ) {
2901                         $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2902                 }
2903
2904                 if ( 'ids' == $q['fields'] ) {
2905                         $this->posts = $wpdb->get_col( $this->request );
2906                         $this->post_count = count( $this->posts );
2907                         $this->set_found_posts( $q, $limits );
2908
2909                         return $this->posts;
2910                 }
2911
2912                 if ( 'id=>parent' == $q['fields'] ) {
2913                         $this->posts = $wpdb->get_results( $this->request );
2914                         $this->post_count = count( $this->posts );
2915                         $this->set_found_posts( $q, $limits );
2916
2917                         $r = array();
2918                         foreach ( $this->posts as $post )
2919                                 $r[ $post->ID ] = $post->post_parent;
2920
2921                         return $r;
2922                 }
2923
2924                 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2925                 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2926
2927                 if ( $split_the_query ) {
2928                         // First get the IDs and then fill in the objects
2929
2930                         $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2931
2932                         $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2933
2934                         $ids = $wpdb->get_col( $this->request );
2935
2936                         if ( $ids ) {
2937                                 $this->posts = $ids;
2938                                 $this->set_found_posts( $q, $limits );
2939                                 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2940                         } else {
2941                                 $this->posts = array();
2942                         }
2943                 } else {
2944                         $this->posts = $wpdb->get_results( $this->request );
2945                         $this->set_found_posts( $q, $limits );
2946                 }
2947
2948                 // Convert to WP_Post objects
2949                 if ( $this->posts )
2950                         $this->posts = array_map( 'get_post', $this->posts );
2951
2952                 // Raw results filter. Prior to status checks.
2953                 if ( !$q['suppress_filters'] )
2954                         $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2955
2956                 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2957                         $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2958                         $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2959                         $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2960                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2961                         $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2962                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2963                         $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2964                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2965                         $this->comments = $wpdb->get_results($comments_request);
2966                         $this->comment_count = count($this->comments);
2967                 }
2968
2969                 // Check post status to determine if post should be displayed.
2970                 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2971                         $status = get_post_status($this->posts[0]);
2972                         $post_status_obj = get_post_status_object($status);
2973                         //$type = get_post_type($this->posts[0]);
2974                         if ( !$post_status_obj->public ) {
2975                                 if ( ! is_user_logged_in() ) {
2976                                         // User must be logged in to view unpublished posts.
2977                                         $this->posts = array();
2978                                 } else {
2979                                         if  ( $post_status_obj->protected ) {
2980                                                 // User must have edit permissions on the draft to preview.
2981                                                 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2982                                                         $this->posts = array();
2983                                                 } else {
2984                                                         $this->is_preview = true;
2985                                                         if ( 'future' != $status )
2986                                                                 $this->posts[0]->post_date = current_time('mysql');
2987                                                 }
2988                                         } elseif ( $post_status_obj->private ) {
2989                                                 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2990                                                         $this->posts = array();
2991                                         } else {
2992                                                 $this->posts = array();
2993                                         }
2994                                 }
2995                         }
2996
2997                         if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2998                                 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
2999                 }
3000
3001                 // Put sticky posts at the top of the posts array
3002                 $sticky_posts = get_option('sticky_posts');
3003                 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
3004                         $num_posts = count($this->posts);
3005                         $sticky_offset = 0;
3006                         // Loop over posts and relocate stickies to the front.
3007                         for ( $i = 0; $i < $num_posts; $i++ ) {
3008                                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
3009                                         $sticky_post = $this->posts[$i];
3010                                         // Remove sticky from current position
3011                                         array_splice($this->posts, $i, 1);
3012                                         // Move to front, after other stickies
3013                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
3014                                         // Increment the sticky offset. The next sticky will be placed at this offset.
3015                                         $sticky_offset++;
3016                                         // Remove post from sticky posts array
3017                                         $offset = array_search($sticky_post->ID, $sticky_posts);
3018                                         unset( $sticky_posts[$offset] );
3019                                 }
3020                         }
3021
3022                         // If any posts have been excluded specifically, Ignore those that are sticky.
3023                         if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3024                                 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3025
3026                         // Fetch sticky posts that weren't in the query results
3027                         if ( !empty($sticky_posts) ) {
3028                                 $stickies = get_posts( array(
3029                                         'post__in' => $sticky_posts,
3030                                         'post_type' => $post_type,
3031                                         'post_status' => 'publish',
3032                                         'nopaging' => true
3033                                 ) );
3034
3035                                 foreach ( $stickies as $sticky_post ) {
3036                                         array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3037                                         $sticky_offset++;
3038                                 }
3039                         }
3040                 }
3041
3042                 if ( !$q['suppress_filters'] )
3043                         $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
3044
3045                 // Ensure that any posts added/modified via one of the filters above are
3046                 // of the type WP_Post and are filtered.
3047                 if ( $this->posts ) {
3048                         $this->post_count = count( $this->posts );
3049
3050                         $this->posts = array_map( 'get_post', $this->posts );
3051
3052                         if ( $q['cache_results'] )
3053                                 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3054
3055                         $this->post = reset( $this->posts );
3056                 } else {
3057                         $this->post_count = 0;
3058                         $this->posts = array();
3059                 }
3060
3061                 return $this->posts;
3062         }
3063
3064         /**
3065          * Set up the amount of found posts and the number of pages (if limit clause was used)
3066          * for the current query.
3067          *
3068          * @since 3.5.0
3069          * @access private
3070          */
3071         function set_found_posts( $q, $limits ) {
3072                 global $wpdb;
3073
3074                 // Bail if posts is an empty array. Continue if posts is an empty string,
3075                 // null, or false to accommodate caching plugins that fill posts later.
3076                 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3077                         return;
3078
3079                 if ( ! empty( $limits ) )
3080                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3081                 else
3082                         $this->found_posts = count( $this->posts );
3083
3084                 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3085
3086                 if ( ! empty( $limits ) )
3087                         $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3088         }
3089
3090         /**
3091          * Set up the next post and iterate current post index.
3092          *
3093          * @since 1.5.0
3094          * @access public
3095          *
3096          * @return WP_Post Next post.
3097          */
3098         function next_post() {
3099
3100                 $this->current_post++;
3101
3102                 $this->post = $this->posts[$this->current_post];
3103                 return $this->post;
3104         }
3105
3106         /**
3107          * Sets up the current post.
3108          *
3109          * Retrieves the next post, sets up the post, sets the 'in the loop'
3110          * property to true.
3111          *
3112          * @since 1.5.0
3113          * @access public
3114          * @uses $post
3115          * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3116          */
3117         function the_post() {
3118                 global $post;
3119                 $this->in_the_loop = true;
3120
3121                 if ( $this->current_post == -1 ) // loop has just started
3122                         do_action_ref_array('loop_start', array(&$this));
3123
3124                 $post = $this->next_post();
3125                 setup_postdata($post);
3126         }
3127
3128         /**
3129          * Whether there are more posts available in the loop.
3130          *
3131          * Calls action 'loop_end', when the loop is complete.
3132          *
3133          * @since 1.5.0
3134          * @access public
3135          * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3136          *
3137          * @return bool True if posts are available, false if end of loop.
3138          */
3139         function have_posts() {
3140                 if ( $this->current_post + 1 < $this->post_count ) {
3141                         return true;
3142                 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3143                         do_action_ref_array('loop_end', array(&$this));
3144                         // Do some cleaning up after the loop
3145                         $this->rewind_posts();
3146                 }
3147
3148                 $this->in_the_loop = false;
3149                 return false;
3150         }
3151
3152         /**
3153          * Rewind the posts and reset post index.
3154          *
3155          * @since 1.5.0
3156          * @access public
3157          */
3158         function rewind_posts() {
3159                 $this->current_post = -1;
3160                 if ( $this->post_count > 0 ) {
3161                         $this->post = $this->posts[0];
3162                 }
3163         }
3164
3165         /**
3166          * Iterate current comment index and return comment object.
3167          *
3168          * @since 2.2.0
3169          * @access public
3170          *
3171          * @return object Comment object.
3172          */
3173         function next_comment() {
3174                 $this->current_comment++;
3175
3176                 $this->comment = $this->comments[$this->current_comment];
3177                 return $this->comment;
3178         }
3179
3180         /**
3181          * Sets up the current comment.
3182          *
3183          * @since 2.2.0
3184          * @access public
3185          * @global object $comment Current comment.
3186          * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3187          */
3188         function the_comment() {
3189                 global $comment;
3190
3191                 $comment = $this->next_comment();
3192
3193                 if ( $this->current_comment == 0 ) {
3194                         do_action('comment_loop_start');
3195                 }
3196         }
3197
3198         /**
3199          * Whether there are more comments available.
3200          *
3201          * Automatically rewinds comments when finished.
3202          *
3203          * @since 2.2.0
3204          * @access public
3205          *
3206          * @return bool True, if more comments. False, if no more posts.
3207          */
3208         function have_comments() {
3209                 if ( $this->current_comment + 1 < $this->comment_count ) {
3210                         return true;
3211                 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3212                         $this->rewind_comments();
3213                 }
3214
3215                 return false;
3216         }
3217
3218         /**
3219          * Rewind the comments, resets the comment index and comment to first.
3220          *
3221          * @since 2.2.0
3222          * @access public
3223          */
3224         function rewind_comments() {
3225                 $this->current_comment = -1;
3226                 if ( $this->comment_count > 0 ) {
3227                         $this->comment = $this->comments[0];
3228                 }
3229         }
3230
3231         /**
3232          * Sets up the WordPress query by parsing query string.
3233          *
3234          * @since 1.5.0
3235          * @access public
3236          *
3237          * @param string $query URL query string.
3238          * @return array List of posts.
3239          */
3240         function query( $query ) {
3241                 $this->init();
3242                 $this->query = $this->query_vars = wp_parse_args( $query );
3243                 return $this->get_posts();
3244         }
3245
3246         /**
3247          * Retrieve queried object.
3248          *
3249          * If queried object is not set, then the queried object will be set from
3250          * the category, tag, taxonomy, posts page, single post, page, or author
3251          * query variable. After it is set up, it will be returned.
3252          *
3253          * @since 1.5.0
3254          * @access public
3255          *
3256          * @return object
3257          */
3258         function get_queried_object() {
3259                 if ( isset($this->queried_object) )
3260                         return $this->queried_object;
3261
3262                 $this->queried_object = null;
3263                 $this->queried_object_id = 0;
3264
3265                 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3266                         if ( $this->is_category ) {
3267                                 $term = get_term( $this->get( 'cat' ), 'category' );
3268                         } elseif ( $this->is_tag ) {
3269                                 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
3270                         } else {
3271                                 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3272                                 $query = reset( $tax_query_in_and );
3273
3274                                 if ( 'term_id' == $query['field'] )
3275                                         $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3276                                 else
3277                                         $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3278                         }
3279
3280                         if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
3281                                 $this->queried_object = $term;
3282                                 $this->queried_object_id = (int) $term->term_id;
3283
3284                                 if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
3285                                         _make_cat_compat( $this->queried_object );
3286                         }
3287                 } elseif ( $this->is_post_type_archive ) {
3288                         $post_type = $this->get( 'post_type' );
3289                         if ( is_array( $post_type ) )
3290                                 $post_type = reset( $post_type );
3291                         $this->queried_object = get_post_type_object( $post_type );
3292                 } elseif ( $this->is_posts_page ) {
3293                         $page_for_posts = get_option('page_for_posts');
3294                         $this->queried_object = get_post( $page_for_posts );
3295                         $this->queried_object_id = (int) $this->queried_object->ID;
3296                 } elseif ( $this->is_singular && !is_null($this->post) ) {
3297                         $this->queried_object = $this->post;
3298                         $this->queried_object_id = (int) $this->post->ID;
3299                 } elseif ( $this->is_author ) {
3300                         $this->queried_object_id = (int) $this->get('author');
3301                         $this->queried_object = get_userdata( $this->queried_object_id );
3302                 }
3303
3304                 return $this->queried_object;
3305         }
3306
3307         /**
3308          * Retrieve ID of the current queried object.
3309          *
3310          * @since 1.5.0
3311          * @access public
3312          *
3313          * @return int
3314          */
3315         function get_queried_object_id() {
3316                 $this->get_queried_object();
3317
3318                 if ( isset($this->queried_object_id) ) {
3319                         return $this->queried_object_id;
3320                 }
3321
3322                 return 0;
3323         }
3324
3325         /**
3326          * Constructor.
3327          *
3328          * Sets up the WordPress query, if parameter is not empty.
3329          *
3330          * @since 1.5.0
3331          * @access public
3332          *
3333          * @param string $query URL query string.
3334          * @return WP_Query
3335          */
3336         function __construct($query = '') {
3337                 if ( ! empty($query) ) {
3338                         $this->query($query);
3339                 }
3340         }
3341
3342         /**
3343          * Is the query for an existing archive page?
3344          *
3345          * Month, Year, Category, Author, Post Type archive...
3346          *
3347          * @since 3.1.0
3348          *
3349          * @return bool
3350          */
3351         function is_archive() {
3352                 return (bool) $this->is_archive;
3353         }
3354
3355         /**
3356          * Is the query for an existing post type archive page?
3357          *
3358          * @since 3.1.0
3359          *
3360          * @param mixed $post_types Optional. Post type or array of posts types to check against.
3361          * @return bool
3362          */
3363         function is_post_type_archive( $post_types = '' ) {
3364                 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3365                         return (bool) $this->is_post_type_archive;
3366
3367                 $post_type = $this->get( 'post_type' );
3368                 if ( is_array( $post_type ) )
3369                         $post_type = reset( $post_type );
3370                 $post_type_object = get_post_type_object( $post_type );
3371
3372                 return in_array( $post_type_object->name, (array) $post_types );
3373         }
3374
3375         /**
3376          * Is the query for an existing attachment page?
3377          *
3378          * @since 3.1.0
3379          *
3380          * @return bool
3381          */
3382         function is_attachment() {
3383                 return (bool) $this->is_attachment;
3384         }
3385
3386         /**
3387          * Is the query for an existing author archive page?
3388          *
3389          * If the $author parameter is specified, this function will additionally
3390          * check if the query is for one of the authors specified.
3391          *
3392          * @since 3.1.0
3393          *
3394          * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3395          * @return bool
3396          */
3397         function is_author( $author = '' ) {
3398                 if ( !$this->is_author )
3399                         return false;
3400
3401                 if ( empty($author) )
3402                         return true;
3403
3404                 $author_obj = $this->get_queried_object();
3405
3406                 $author = (array) $author;
3407
3408                 if ( in_array( $author_obj->ID, $author ) )
3409                         return true;
3410                 elseif ( in_array( $author_obj->nickname, $author ) )
3411                         return true;
3412                 elseif ( in_array( $author_obj->user_nicename, $author ) )
3413                         return true;
3414
3415                 return false;
3416         }
3417
3418         /**
3419          * Is the query for an existing category archive page?
3420          *
3421          * If the $category parameter is specified, this function will additionally
3422          * check if the query is for one of the categories specified.
3423          *
3424          * @since 3.1.0
3425          *
3426          * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3427          * @return bool
3428          */
3429         function is_category( $category = '' ) {
3430                 if ( !$this->is_category )
3431                         return false;
3432
3433                 if ( empty($category) )
3434                         return true;
3435
3436                 $cat_obj = $this->get_queried_object();
3437
3438                 $category = (array) $category;
3439
3440                 if ( in_array( $cat_obj->term_id, $category ) )
3441                         return true;
3442                 elseif ( in_array( $cat_obj->name, $category ) )
3443                         return true;
3444                 elseif ( in_array( $cat_obj->slug, $category ) )
3445                         return true;
3446
3447                 return false;
3448         }
3449
3450         /**
3451          * Is the query for an existing tag archive page?
3452          *
3453          * If the $tag parameter is specified, this function will additionally
3454          * check if the query is for one of the tags specified.
3455          *
3456          * @since 3.1.0
3457          *
3458          * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3459          * @return bool
3460          */
3461         function is_tag( $tag = '' ) {
3462                 if ( ! $this->is_tag )
3463                         return false;
3464
3465                 if ( empty( $tag ) )
3466                         return true;
3467
3468                 $tag_obj = $this->get_queried_object();
3469
3470                 $tag = (array) $tag;
3471
3472                 if (