]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/query.php
WordPress 3.7.1-scripts
[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']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1771                         $q['cat'] = ''.urldecode($q['cat']).'';
1772                         $q['cat'] = addslashes_gpc($q['cat']);
1773                         $cat_array = preg_split('/[,\s]+/', $q['cat']);
1774                         $q['cat'] = '';
1775                         $req_cats = array();
1776                         foreach ( (array) $cat_array as $cat ) {
1777                                 $cat = intval($cat);
1778                                 $req_cats[] = $cat;
1779                                 $in = ($cat > 0);
1780                                 $cat = abs($cat);
1781                                 if ( $in ) {
1782                                         $q['category__in'][] = $cat;
1783                                         $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1784                                 } else {
1785                                         $q['category__not_in'][] = $cat;
1786                                         $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1787                                 }
1788                         }
1789                         $q['cat'] = implode(',', $req_cats);
1790                 }
1791
1792                 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1793                         $q['category__and'] = (array) $q['category__and'];
1794                         if ( ! isset( $q['category__in'] ) )
1795                                 $q['category__in'] = array();
1796                         $q['category__in'][] = absint( reset( $q['category__and'] ) );
1797                         unset( $q['category__and'] );
1798                 }
1799
1800                 if ( ! empty( $q['category__in'] ) ) {
1801                         $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1802                         $tax_query[] = array(
1803                                 'taxonomy' => 'category',
1804                                 'terms' => $q['category__in'],
1805                                 'field' => 'term_id',
1806                                 'include_children' => false
1807                         );
1808                 }
1809
1810                 if ( ! empty($q['category__not_in']) ) {
1811                         $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1812                         $tax_query[] = array(
1813                                 'taxonomy' => 'category',
1814                                 'terms' => $q['category__not_in'],
1815                                 'operator' => 'NOT IN',
1816                                 'include_children' => false
1817                         );
1818                 }
1819
1820                 if ( ! empty($q['category__and']) ) {
1821                         $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1822                         $tax_query[] = array(
1823                                 'taxonomy' => 'category',
1824                                 'terms' => $q['category__and'],
1825                                 'field' => 'term_id',
1826                                 'operator' => 'AND',
1827                                 'include_children' => false
1828                         );
1829                 }
1830
1831                 // Tag stuff
1832                 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1833                         if ( strpos($q['tag'], ',') !== false ) {
1834                                 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1835                                 foreach ( (array) $tags as $tag ) {
1836                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1837                                         $q['tag_slug__in'][] = $tag;
1838                                 }
1839                         } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1840                                 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1841                                 foreach ( (array) $tags as $tag ) {
1842                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1843                                         $q['tag_slug__and'][] = $tag;
1844                                 }
1845                         } else {
1846                                 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1847                                 $q['tag_slug__in'][] = $q['tag'];
1848                         }
1849                 }
1850
1851                 if ( !empty($q['tag_id']) ) {
1852                         $q['tag_id'] = absint( $q['tag_id'] );
1853                         $tax_query[] = array(
1854                                 'taxonomy' => 'post_tag',
1855                                 'terms' => $q['tag_id']
1856                         );
1857                 }
1858
1859                 if ( !empty($q['tag__in']) ) {
1860                         $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1861                         $tax_query[] = array(
1862                                 'taxonomy' => 'post_tag',
1863                                 'terms' => $q['tag__in']
1864                         );
1865                 }
1866
1867                 if ( !empty($q['tag__not_in']) ) {
1868                         $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1869                         $tax_query[] = array(
1870                                 'taxonomy' => 'post_tag',
1871                                 'terms' => $q['tag__not_in'],
1872                                 'operator' => 'NOT IN'
1873                         );
1874                 }
1875
1876                 if ( !empty($q['tag__and']) ) {
1877                         $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1878                         $tax_query[] = array(
1879                                 'taxonomy' => 'post_tag',
1880                                 'terms' => $q['tag__and'],
1881                                 'operator' => 'AND'
1882                         );
1883                 }
1884
1885                 if ( !empty($q['tag_slug__in']) ) {
1886                         $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1887                         $tax_query[] = array(
1888                                 'taxonomy' => 'post_tag',
1889                                 'terms' => $q['tag_slug__in'],
1890                                 'field' => 'slug'
1891                         );
1892                 }
1893
1894                 if ( !empty($q['tag_slug__and']) ) {
1895                         $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1896                         $tax_query[] = array(
1897                                 'taxonomy' => 'post_tag',
1898                                 'terms' => $q['tag_slug__and'],
1899                                 'field' => 'slug',
1900                                 'operator' => 'AND'
1901                         );
1902                 }
1903
1904                 $this->tax_query = new WP_Tax_Query( $tax_query );
1905
1906                 do_action( 'parse_tax_query', $this );
1907         }
1908
1909         /**
1910          * Generate SQL for the WHERE clause based on passed search terms.
1911          *
1912          * @since 3.7.0
1913          *
1914          * @global type $wpdb
1915          * @param array $q Query variables.
1916          */
1917         protected function parse_search( &$q ) {
1918                 global $wpdb;
1919
1920                 $search = '';
1921
1922                 // added slashes screw with quote grouping when done early, so done later
1923                 $q['s'] = stripslashes( $q['s'] );
1924                 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1925                         $q['s'] = urldecode( $q['s'] );
1926                 // there are no line breaks in <input /> fields
1927                 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1928                 $q['search_terms_count'] = 1;
1929                 if ( ! empty( $q['sentence'] ) ) {
1930                         $q['search_terms'] = array( $q['s'] );
1931                 } else {
1932                         if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1933                                 $q['search_terms_count'] = count( $matches[0] );
1934                                 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1935                                 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1936                                 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1937                                         $q['search_terms'] = array( $q['s'] );
1938                         } else {
1939                                 $q['search_terms'] = array( $q['s'] );
1940                         }
1941                 }
1942
1943                 $n = ! empty( $q['exact'] ) ? '' : '%';
1944                 $searchand = '';
1945                 $q['search_orderby_title'] = array();
1946                 foreach ( $q['search_terms'] as $term ) {
1947                         $term = like_escape( esc_sql( $term ) );
1948                         if ( $n )
1949                                 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1950
1951                         $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1952                         $searchand = ' AND ';
1953                 }
1954
1955                 if ( ! empty( $search ) ) {
1956                         $search = " AND ({$search}) ";
1957                         if ( ! is_user_logged_in() )
1958                                 $search .= " AND ($wpdb->posts.post_password = '') ";
1959                 }
1960
1961                 return $search;
1962         }
1963
1964         /**
1965          * Check if the terms are suitable for searching.
1966          *
1967          * Uses an array of stopwords (terms) that are excluded from the separate
1968          * term matching when searching for posts. The list of English stopwords is
1969          * the approximate search engines list, and is translatable.
1970          *
1971          * @since 3.7.0
1972          *
1973          * @param array Terms to check.
1974          * @return array Terms that are not stopwords.
1975          */
1976         protected function parse_search_terms( $terms ) {
1977                 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
1978                 $checked = array();
1979
1980                 $stopwords = $this->get_search_stopwords();
1981
1982                 foreach ( $terms as $term ) {
1983                         // keep before/after spaces when term is for exact match
1984                         if ( preg_match( '/^".+"$/', $term ) )
1985                                 $term = trim( $term, "\"'" );
1986                         else
1987                                 $term = trim( $term, "\"' " );
1988
1989                         // Avoid single A-Z.
1990                         if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
1991                                 continue;
1992
1993                         if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
1994                                 continue;
1995
1996                         $checked[] = $term;
1997                 }
1998
1999                 return $checked;
2000         }
2001
2002         /**
2003          * Retrieve stopwords used when parsing search terms.
2004          *
2005          * @since 3.7.0
2006          *
2007          * @return array Stopwords.
2008          */
2009         protected function get_search_stopwords() {
2010                 if ( isset( $this->stopwords ) )
2011                         return $this->stopwords;
2012
2013                 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2014                  * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2015                  * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2016                  */
2017                 $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',
2018                         'Comma-separated list of search stopwords in your language' ) );
2019
2020                 foreach( $words as $word ) {
2021                         $word = trim( $word, "\r\n\t " );
2022                         if ( $word )
2023                                 $stopwords[] = $word;
2024                 }
2025
2026                 /**
2027                  * Filter stopwords used when parsing search terms.
2028                  *
2029                  * @since 3.7.0
2030                  *
2031                  * @param array $stopwords Stopwords.
2032                  */
2033                 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2034                 return $this->stopwords;
2035         }
2036
2037         /**
2038          * Generate SQL for the ORDER BY condition based on passed search terms.
2039          *
2040          * @global wpdb $wpdb
2041          * @param array $q Query variables.
2042          * @return string ORDER BY clause.
2043          */
2044         protected function parse_search_order( &$q ) {
2045                 global $wpdb;
2046
2047                 $search_orderby = '';
2048
2049                 if ( $q['search_terms_count'] > 1 ) {
2050                         $num_terms = count( $q['search_orderby_title'] );
2051                         $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2052
2053                         $search_orderby = '(CASE ';
2054                         // sentence match in 'post_title'
2055                         $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2056
2057                         // sanity limit, sort as sentence when more than 6 terms
2058                         // (few searches are longer than 6 terms and most titles are not)
2059                         if ( $num_terms < 7 ) {
2060                                 // all words in title
2061                                 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2062                                 // any word in title, not needed when $num_terms == 1
2063                                 if ( $num_terms > 1 )
2064                                         $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2065                         }
2066
2067                         // sentence match in 'post_content'
2068                         $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2069                         $search_orderby .= 'ELSE 5 END)';
2070                 } else {
2071                         // single word or sentence search
2072                         $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2073                 }
2074
2075                 return $search_orderby;
2076         }
2077
2078         /**
2079          * Sets the 404 property and saves whether query is feed.
2080          *
2081          * @since 2.0.0
2082          * @access public
2083          */
2084         function set_404() {
2085                 $is_feed = $this->is_feed;
2086
2087                 $this->init_query_flags();
2088                 $this->is_404 = true;
2089
2090                 $this->is_feed = $is_feed;
2091         }
2092
2093         /**
2094          * Retrieve query variable.
2095          *
2096          * @since 1.5.0
2097          * @access public
2098          *
2099          * @param string $query_var Query variable key.
2100          * @return mixed
2101          */
2102         function get($query_var) {
2103                 if ( isset($this->query_vars[$query_var]) )
2104                         return $this->query_vars[$query_var];
2105
2106                 return '';
2107         }
2108
2109         /**
2110          * Set query variable.
2111          *
2112          * @since 1.5.0
2113          * @access public
2114          *
2115          * @param string $query_var Query variable key.
2116          * @param mixed $value Query variable value.
2117          */
2118         function set($query_var, $value) {
2119                 $this->query_vars[$query_var] = $value;
2120         }
2121
2122         /**
2123          * Retrieve the posts based on query variables.
2124          *
2125          * There are a few filters and actions that can be used to modify the post
2126          * database query.
2127          *
2128          * @since 1.5.0
2129          * @access public
2130          * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2131          *
2132          * @return array List of posts.
2133          */
2134         function get_posts() {
2135                 global $wpdb;
2136
2137                 $this->parse_query();
2138
2139                 do_action_ref_array('pre_get_posts', array(&$this));
2140
2141                 // Shorthand.
2142                 $q = &$this->query_vars;
2143
2144                 // Fill again in case pre_get_posts unset some vars.
2145                 $q = $this->fill_query_vars($q);
2146
2147                 // Parse meta query
2148                 $this->meta_query = new WP_Meta_Query();
2149                 $this->meta_query->parse_query_vars( $q );
2150
2151                 // Set a flag if a pre_get_posts hook changed the query vars.
2152                 $hash = md5( serialize( $this->query_vars ) );
2153                 if ( $hash != $this->query_vars_hash ) {
2154                         $this->query_vars_changed = true;
2155                         $this->query_vars_hash = $hash;
2156                 }
2157                 unset($hash);
2158
2159                 // First let's clear some variables
2160                 $distinct = '';
2161                 $whichauthor = '';
2162                 $whichmimetype = '';
2163                 $where = '';
2164                 $limits = '';
2165                 $join = '';
2166                 $search = '';
2167                 $groupby = '';
2168                 $fields = '';
2169                 $post_status_join = false;
2170                 $page = 1;
2171
2172                 if ( isset( $q['caller_get_posts'] ) ) {
2173                         _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2174                         if ( !isset( $q['ignore_sticky_posts'] ) )
2175                                 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2176                 }
2177
2178                 if ( !isset( $q['ignore_sticky_posts'] ) )
2179                         $q['ignore_sticky_posts'] = false;
2180
2181                 if ( !isset($q['suppress_filters']) )
2182                         $q['suppress_filters'] = false;
2183
2184                 if ( !isset($q['cache_results']) ) {
2185                         if ( wp_using_ext_object_cache() )
2186                                 $q['cache_results'] = false;
2187                         else
2188                                 $q['cache_results'] = true;
2189                 }
2190
2191                 if ( !isset($q['update_post_term_cache']) )
2192                         $q['update_post_term_cache'] = true;
2193
2194                 if ( !isset($q['update_post_meta_cache']) )
2195                         $q['update_post_meta_cache'] = true;
2196
2197                 if ( !isset($q['post_type']) ) {
2198                         if ( $this->is_search )
2199                                 $q['post_type'] = 'any';
2200                         else
2201                                 $q['post_type'] = '';
2202                 }
2203                 $post_type = $q['post_type'];
2204                 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
2205                         $q['posts_per_page'] = get_option('posts_per_page');
2206                 if ( isset($q['showposts']) && $q['showposts'] ) {
2207                         $q['showposts'] = (int) $q['showposts'];
2208                         $q['posts_per_page'] = $q['showposts'];
2209                 }
2210                 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2211                         $q['posts_per_page'] = $q['posts_per_archive_page'];
2212                 if ( !isset($q['nopaging']) ) {
2213                         if ( $q['posts_per_page'] == -1 ) {
2214                                 $q['nopaging'] = true;
2215                         } else {
2216                                 $q['nopaging'] = false;
2217                         }
2218                 }
2219                 if ( $this->is_feed ) {
2220                         $q['posts_per_page'] = get_option('posts_per_rss');
2221                         $q['nopaging'] = false;
2222                 }
2223                 $q['posts_per_page'] = (int) $q['posts_per_page'];
2224                 if ( $q['posts_per_page'] < -1 )
2225                         $q['posts_per_page'] = abs($q['posts_per_page']);
2226                 else if ( $q['posts_per_page'] == 0 )
2227                         $q['posts_per_page'] = 1;
2228
2229                 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2230                         $q['comments_per_page'] = get_option('comments_per_page');
2231
2232                 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2233                         $this->is_page = true;
2234                         $this->is_home = false;
2235                         $q['page_id'] = get_option('page_on_front');
2236                 }
2237
2238                 if ( isset($q['page']) ) {
2239                         $q['page'] = trim($q['page'], '/');
2240                         $q['page'] = absint($q['page']);
2241                 }
2242
2243                 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2244                 if ( isset($q['no_found_rows']) )
2245                         $q['no_found_rows'] = (bool) $q['no_found_rows'];
2246                 else
2247                         $q['no_found_rows'] = false;
2248
2249                 switch ( $q['fields'] ) {
2250                         case 'ids':
2251                                 $fields = "$wpdb->posts.ID";
2252                                 break;
2253                         case 'id=>parent':
2254                                 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2255                                 break;
2256                         default:
2257                                 $fields = "$wpdb->posts.*";
2258                 }
2259
2260                 if ( '' !== $q['menu_order'] )
2261                         $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2262
2263                 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2264                 if ( $q['m'] ) {
2265                         $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2266                         if ( strlen($q['m']) > 5 )
2267                                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2268                         if ( strlen($q['m']) > 7 )
2269                                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2270                         if ( strlen($q['m']) > 9 )
2271                                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2272                         if ( strlen($q['m']) > 11 )
2273                                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2274                         if ( strlen($q['m']) > 13 )
2275                                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2276                 }
2277
2278                 // Handle the other individual date parameters
2279                 $date_parameters = array();
2280
2281                 if ( '' !== $q['hour'] )
2282                         $date_parameters['hour'] = $q['hour'];
2283
2284                 if ( '' !== $q['minute'] )
2285                         $date_parameters['minute'] = $q['minute'];
2286
2287                 if ( '' !== $q['second'] )
2288                         $date_parameters['second'] = $q['second'];
2289
2290                 if ( $q['year'] )
2291                         $date_parameters['year'] = $q['year'];
2292
2293                 if ( $q['monthnum'] )
2294                         $date_parameters['monthnum'] = $q['monthnum'];
2295
2296                 if ( $q['w'] )
2297                         $date_parameters['week'] = $q['w'];
2298
2299                 if ( $q['day'] )
2300                         $date_parameters['day'] = $q['day'];
2301
2302                 if ( $date_parameters ) {
2303                         $date_query = new WP_Date_Query( array( $date_parameters ) );
2304                         $where .= $date_query->get_sql();
2305                 }
2306                 unset( $date_parameters, $date_query );
2307
2308                 // Handle complex date queries
2309                 if ( ! empty( $q['date_query'] ) ) {
2310                         $this->date_query = new WP_Date_Query( $q['date_query'] );
2311                         $where .= $this->date_query->get_sql();
2312                 }
2313
2314
2315                 // If we've got a post_type AND it's not "any" post_type.
2316                 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2317                         foreach ( (array)$q['post_type'] as $_post_type ) {
2318                                 $ptype_obj = get_post_type_object($_post_type);
2319                                 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2320                                         continue;
2321
2322                                 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2323                                         // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2324                                         $q['name'] = $q[ $ptype_obj->query_var ];
2325                                 } else {
2326                                         // Hierarchical post_types will operate through the
2327                                         $q['pagename'] = $q[ $ptype_obj->query_var ];
2328                                         $q['name'] = '';
2329                                 }
2330
2331                                 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2332                                 break;
2333                         } //end foreach
2334                         unset($ptype_obj);
2335                 }
2336
2337                 if ( '' != $q['name'] ) {
2338                         $q['name'] = sanitize_title_for_query( $q['name'] );
2339                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2340                 } elseif ( '' != $q['pagename'] ) {
2341                         if ( isset($this->queried_object_id) ) {
2342                                 $reqpage = $this->queried_object_id;
2343                         } else {
2344                                 if ( 'page' != $q['post_type'] ) {
2345                                         foreach ( (array)$q['post_type'] as $_post_type ) {
2346                                                 $ptype_obj = get_post_type_object($_post_type);
2347                                                 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2348                                                         continue;
2349
2350                                                 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2351                                                 if ( $reqpage )
2352                                                         break;
2353                                         }
2354                                         unset($ptype_obj);
2355                                 } else {
2356                                         $reqpage = get_page_by_path($q['pagename']);
2357                                 }
2358                                 if ( !empty($reqpage) )
2359                                         $reqpage = $reqpage->ID;
2360                                 else
2361                                         $reqpage = 0;
2362                         }
2363
2364                         $page_for_posts = get_option('page_for_posts');
2365                         if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2366                                 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2367                                 $q['name'] = $q['pagename'];
2368                                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2369                                 $reqpage_obj = get_post( $reqpage );
2370                                 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2371                                         $this->is_attachment = true;
2372                                         $post_type = $q['post_type'] = 'attachment';
2373                                         $this->is_page = true;
2374                                         $q['attachment_id'] = $reqpage;
2375                                 }
2376                         }
2377                 } elseif ( '' != $q['attachment'] ) {
2378                         $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2379                         $q['name'] = $q['attachment'];
2380                         $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2381                 }
2382
2383
2384                 if ( intval($q['comments_popup']) )
2385                         $q['p'] = absint($q['comments_popup']);
2386
2387                 // If an attachment is requested by number, let it supersede any post number.
2388                 if ( $q['attachment_id'] )
2389                         $q['p'] = absint($q['attachment_id']);
2390
2391                 // If a post number is specified, load that post
2392                 if ( $q['p'] ) {
2393                         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2394                 } elseif ( $q['post__in'] ) {
2395                         $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2396                         $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2397                 } elseif ( $q['post__not_in'] ) {
2398                         $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
2399                         $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2400                 }
2401
2402                 if ( is_numeric( $q['post_parent'] ) ) {
2403                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2404                 } elseif ( $q['post_parent__in'] ) {
2405                         $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2406                         $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2407                 } elseif ( $q['post_parent__not_in'] ) {
2408                         $post_parent__not_in = implode( ',',  array_map( 'absint', $q['post_parent__not_in'] ) );
2409                         $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2410                 }
2411
2412                 if ( $q['page_id'] ) {
2413                         if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2414                                 $q['p'] = $q['page_id'];
2415                                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2416                         }
2417                 }
2418
2419                 // If a search pattern is specified, load the posts that match.
2420                 if ( ! empty( $q['s'] ) )
2421                         $search = $this->parse_search( $q );
2422
2423                 /**
2424                  * Filter the search SQL that is used in the WHERE clause of WP_Query.
2425                  *
2426                  * @since 3.0.0
2427                  *
2428                  * @param string   $search Search SQL for WHERE clause.
2429                  * @param WP_Query $this   The current WP_Query object.
2430                  */
2431                 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2432
2433                 // Taxonomies
2434                 if ( !$this->is_singular ) {
2435                         $this->parse_tax_query( $q );
2436
2437                         $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2438
2439                         $join .= $clauses['join'];
2440                         $where .= $clauses['where'];
2441                 }
2442
2443                 if ( $this->is_tax ) {
2444                         if ( empty($post_type) ) {
2445                                 // Do a fully inclusive search for currently registered post types of queried taxonomies
2446                                 $post_type = array();
2447                                 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2448                                 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2449                                         $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2450                                         if ( array_intersect( $taxonomies, $object_taxonomies ) )
2451                                                 $post_type[] = $pt;
2452                                 }
2453                                 if ( ! $post_type )
2454                                         $post_type = 'any';
2455                                 elseif ( count( $post_type ) == 1 )
2456                                         $post_type = $post_type[0];
2457
2458                                 $post_status_join = true;
2459                         } elseif ( in_array('attachment', (array) $post_type) ) {
2460                                 $post_status_join = true;
2461                         }
2462                 }
2463
2464                 // Back-compat
2465                 if ( !empty($this->tax_query->queries) ) {
2466                         $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2467                         if ( !empty( $tax_query_in_and ) ) {
2468                                 if ( !isset( $q['taxonomy'] ) ) {
2469                                         foreach ( $tax_query_in_and as $a_tax_query ) {
2470                                                 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2471                                                         $q['taxonomy'] = $a_tax_query['taxonomy'];
2472                                                         if ( 'slug' == $a_tax_query['field'] )
2473                                                                 $q['term'] = $a_tax_query['terms'][0];
2474                                                         else
2475                                                                 $q['term_id'] = $a_tax_query['terms'][0];
2476
2477                                                         break;
2478                                                 }
2479                                         }
2480                                 }
2481
2482                                 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2483                                 if ( ! empty( $cat_query ) ) {
2484                                         $cat_query = reset( $cat_query );
2485
2486                                         if ( ! empty( $cat_query['terms'][0] ) ) {
2487                                                 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2488                                                 if ( $the_cat ) {
2489                                                         $this->set( 'cat', $the_cat->term_id );
2490                                                         $this->set( 'category_name', $the_cat->slug );
2491                                                 }
2492                                                 unset( $the_cat );
2493                                         }
2494                                 }
2495                                 unset( $cat_query );
2496
2497                                 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2498                                 if ( ! empty( $tag_query ) ) {
2499                                         $tag_query = reset( $tag_query );
2500
2501                                         if ( ! empty( $tag_query['terms'][0] ) ) {
2502                                                 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2503                                                 if ( $the_tag )
2504                                                         $this->set( 'tag_id', $the_tag->term_id );
2505                                                 unset( $the_tag );
2506                                         }
2507                                 }
2508                                 unset( $tag_query );
2509                         }
2510                 }
2511
2512                 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2513                         $groupby = "{$wpdb->posts}.ID";
2514                 }
2515
2516                 // Author/user stuff
2517
2518                 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2519                         $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2520                         $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2521                         foreach ( $authors as $author ) {
2522                                 $key = $author > 0 ? 'author__in' : 'author__not_in';
2523                                 $q[$key][] = abs( $author );
2524                         }
2525                         $q['author'] = implode( ',', $authors );
2526                 }
2527
2528                 if ( ! empty( $q['author__not_in'] ) ) {
2529                         $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2530                         $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2531                 } elseif ( ! empty( $q['author__in'] ) ) {
2532                         $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2533                         $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2534                 }
2535
2536                 // Author stuff for nice URLs
2537
2538                 if ( '' != $q['author_name'] ) {
2539                         if ( strpos($q['author_name'], '/') !== false ) {
2540                                 $q['author_name'] = explode('/', $q['author_name']);
2541                                 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2542                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2543                                 } else {
2544                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2545                                 }
2546                         }
2547                         $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2548                         $q['author'] = get_user_by('slug', $q['author_name']);
2549                         if ( $q['author'] )
2550                                 $q['author'] = $q['author']->ID;
2551                         $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2552                 }
2553
2554                 // MIME-Type stuff for attachment browsing
2555
2556                 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2557                         $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2558
2559                 $where .= $search . $whichauthor . $whichmimetype;
2560
2561                 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2562                         $q['order'] = 'DESC';
2563
2564                 // Order by
2565                 if ( empty($q['orderby']) ) {
2566                         $orderby = "$wpdb->posts.post_date " . $q['order'];
2567                 } elseif ( 'none' == $q['orderby'] ) {
2568                         $orderby = '';
2569                 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2570                         $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2571                 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2572                         $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2573                 } else {
2574                         // Used to filter values
2575                         $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2576                         if ( !empty($q['meta_key']) ) {
2577                                 $allowed_keys[] = $q['meta_key'];
2578                                 $allowed_keys[] = 'meta_value';
2579                                 $allowed_keys[] = 'meta_value_num';
2580                         }
2581                         $q['orderby'] = urldecode($q['orderby']);
2582                         $q['orderby'] = addslashes_gpc($q['orderby']);
2583
2584                         $orderby_array = array();
2585                         foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2586                                 // Only allow certain values for safety
2587                                 if ( ! in_array($orderby, $allowed_keys) )
2588                                         continue;
2589
2590                                 switch ( $orderby ) {
2591                                         case 'menu_order':
2592                                                 $orderby = "$wpdb->posts.menu_order";
2593                                                 break;
2594                                         case 'ID':
2595                                                 $orderby = "$wpdb->posts.ID";
2596                                                 break;
2597                                         case 'rand':
2598                                                 $orderby = 'RAND()';
2599                                                 break;
2600                                         case $q['meta_key']:
2601                                         case 'meta_value':
2602                                                 if ( isset( $q['meta_type'] ) ) {
2603                                                         $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2604                                                         $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2605                                                 } else {
2606                                                         $orderby = "$wpdb->postmeta.meta_value";
2607                                                 }
2608                                                 break;
2609                                         case 'meta_value_num':
2610                                                 $orderby = "$wpdb->postmeta.meta_value+0";
2611                                                 break;
2612                                         case 'comment_count':
2613                                                 $orderby = "$wpdb->posts.comment_count";
2614                                                 break;
2615                                         default:
2616                                                 $orderby = "$wpdb->posts.post_" . $orderby;
2617                                 }
2618
2619                                 $orderby_array[] = $orderby;
2620                         }
2621                         $orderby = implode( ',', $orderby_array );
2622
2623                         if ( empty( $orderby ) )
2624                                 $orderby = "$wpdb->posts.post_date ".$q['order'];
2625                         else
2626                                 $orderby .= " {$q['order']}";
2627                 }
2628
2629                 // Order search results by relevance only when another "orderby" is not specified in the query.
2630                 if ( ! empty( $q['s'] ) ) {
2631                         $search_orderby = '';
2632                         if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2633                                 $search_orderby = $this->parse_search_order( $q );
2634
2635                         /**
2636                          * Filter the ORDER BY used when ordering search results.
2637                          *
2638                          * @since 3.7.0
2639                          *
2640                          * @param string   $search_orderby The ORDER BY clause.
2641                          * @param WP_Query $this           The current WP_Query instance.
2642                          */
2643                         $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2644                         if ( $search_orderby )
2645                                 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2646                 }
2647
2648                 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2649                         $post_type_cap = 'multiple_post_type';
2650                 } else {
2651                         if ( is_array( $post_type ) )
2652                                 $post_type = reset( $post_type );
2653                         $post_type_object = get_post_type_object( $post_type );
2654                         if ( empty( $post_type_object ) )
2655                                 $post_type_cap = $post_type;
2656                 }
2657
2658                 if ( 'any' == $post_type ) {
2659                         $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2660                         if ( empty( $in_search_post_types ) )
2661                                 $where .= ' AND 1=0 ';
2662                         else
2663                                 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2664                 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2665                         $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2666                 } elseif ( ! empty( $post_type ) ) {
2667                         $where .= " AND $wpdb->posts.post_type = '$post_type'";
2668                         $post_type_object = get_post_type_object ( $post_type );
2669                 } elseif ( $this->is_attachment ) {
2670                         $where .= " AND $wpdb->posts.post_type = 'attachment'";
2671                         $post_type_object = get_post_type_object ( 'attachment' );
2672                 } elseif ( $this->is_page ) {
2673                         $where .= " AND $wpdb->posts.post_type = 'page'";
2674                         $post_type_object = get_post_type_object ( 'page' );
2675                 } else {
2676                         $where .= " AND $wpdb->posts.post_type = 'post'";
2677                         $post_type_object = get_post_type_object ( 'post' );
2678                 }
2679
2680                 $edit_cap = 'edit_post';
2681                 $read_cap = 'read_post';
2682
2683                 if ( ! empty( $post_type_object ) ) {
2684                         $edit_others_cap = $post_type_object->cap->edit_others_posts;
2685                         $read_private_cap = $post_type_object->cap->read_private_posts;
2686                 } else {
2687                         $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2688                         $read_private_cap = 'read_private_' . $post_type_cap . 's';
2689                 }
2690
2691                 $user_id = get_current_user_id();
2692
2693                 if ( ! empty( $q['post_status'] ) ) {
2694                         $statuswheres = array();
2695                         $q_status = $q['post_status'];
2696                         if ( ! is_array( $q_status ) )
2697                                 $q_status = explode(',', $q_status);
2698                         $r_status = array();
2699                         $p_status = array();
2700                         $e_status = array();
2701                         if ( in_array('any', $q_status) ) {
2702                                 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2703                                         $e_status[] = "$wpdb->posts.post_status <> '$status'";
2704                         } else {
2705                                 foreach ( get_post_stati() as $status ) {
2706                                         if ( in_array( $status, $q_status ) ) {
2707                                                 if ( 'private' == $status )
2708                                                         $p_status[] = "$wpdb->posts.post_status = '$status'";
2709                                                 else
2710                                                         $r_status[] = "$wpdb->posts.post_status = '$status'";
2711                                         }
2712                                 }
2713                         }
2714
2715                         if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2716                                 $r_status = array_merge($r_status, $p_status);
2717                                 unset($p_status);
2718                         }
2719
2720                         if ( !empty($e_status) ) {
2721                                 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2722                         }
2723                         if ( !empty($r_status) ) {
2724                                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2725                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2726                                 else
2727                                         $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2728                         }
2729                         if ( !empty($p_status) ) {
2730                                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2731                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2732                                 else
2733                                         $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2734                         }
2735                         if ( $post_status_join ) {
2736                                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2737                                 foreach ( $statuswheres as $index => $statuswhere )
2738                                         $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2739                         }
2740                         foreach ( $statuswheres as $statuswhere )
2741                                 $where .= " AND $statuswhere";
2742                 } elseif ( !$this->is_singular ) {
2743                         $where .= " AND ($wpdb->posts.post_status = 'publish'";
2744
2745                         // Add public states.
2746                         $public_states = get_post_stati( array('public' => true) );
2747                         foreach ( (array) $public_states as $state ) {
2748                                 if ( 'publish' == $state ) // Publish is hard-coded above.
2749                                         continue;
2750                                 $where .= " OR $wpdb->posts.post_status = '$state'";
2751                         }
2752
2753                         if ( $this->is_admin ) {
2754                                 // Add protected states that should show in the admin all list.
2755                                 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2756                                 foreach ( (array) $admin_all_states as $state )
2757                                         $where .= " OR $wpdb->posts.post_status = '$state'";
2758                         }
2759
2760                         if ( is_user_logged_in() ) {
2761                                 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2762                                 $private_states = get_post_stati( array('private' => true) );
2763                                 foreach ( (array) $private_states as $state )
2764                                         $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'";
2765                         }
2766
2767                         $where .= ')';
2768                 }
2769
2770                 if ( !empty( $this->meta_query->queries ) ) {
2771                         $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2772                         $join .= $clauses['join'];
2773                         $where .= $clauses['where'];
2774                 }
2775
2776                 // Apply filters on where and join prior to paging so that any
2777                 // manipulations to them are reflected in the paging by day queries.
2778                 if ( !$q['suppress_filters'] ) {
2779                         $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2780                         $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2781                 }
2782
2783                 // Paging
2784                 if ( empty($q['nopaging']) && !$this->is_singular ) {
2785                         $page = absint($q['paged']);
2786                         if ( !$page )
2787                                 $page = 1;
2788
2789                         if ( empty($q['offset']) ) {
2790                                 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2791                         } else { // we're ignoring $page and using 'offset'
2792                                 $q['offset'] = absint($q['offset']);
2793                                 $pgstrt = $q['offset'] . ', ';
2794                         }
2795                         $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2796                 }
2797
2798                 // Comments feeds
2799                 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2800                         if ( $this->is_archive || $this->is_search ) {
2801                                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2802                                 $cwhere = "WHERE comment_approved = '1' $where";
2803                                 $cgroupby = "$wpdb->comments.comment_id";
2804                         } else { // Other non singular e.g. front
2805                                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2806                                 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2807                                 $cgroupby = '';
2808                         }
2809
2810                         if ( !$q['suppress_filters'] ) {
2811                                 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2812                                 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2813                                 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2814                                 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2815                                 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2816                         }
2817                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2818                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2819
2820                         $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2821                         $this->comment_count = count($this->comments);
2822
2823                         $post_ids = array();
2824
2825                         foreach ( $this->comments as $comment )
2826                                 $post_ids[] = (int) $comment->comment_post_ID;
2827
2828                         $post_ids = join(',', $post_ids);
2829                         $join = '';
2830                         if ( $post_ids )
2831                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2832                         else
2833                                 $where = "AND 0";
2834                 }
2835
2836                 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2837
2838                 // Apply post-paging filters on where and join. Only plugins that
2839                 // manipulate paging queries should use these hooks.
2840                 if ( !$q['suppress_filters'] ) {
2841                         $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2842                         $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ) );
2843                         $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
2844                         $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ) );
2845                         $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
2846                         $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) );
2847                         $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) );
2848
2849                         // Filter all clauses at once, for convenience
2850                         $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2851                         foreach ( $pieces as $piece )
2852                                 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2853                 }
2854
2855                 // Announce current selection parameters. For use by caching plugins.
2856                 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2857
2858                 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2859                 if ( !$q['suppress_filters'] ) {
2860                         $where          = apply_filters_ref_array( 'posts_where_request',               array( $where, &$this ) );
2861                         $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ) );
2862                         $join           = apply_filters_ref_array( 'posts_join_request',                array( $join, &$this ) );
2863                         $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ) );
2864                         $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
2865                         $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) );
2866                         $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) );
2867
2868                         // Filter all clauses at once, for convenience
2869                         $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2870                         foreach ( $pieces as $piece )
2871                                 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2872                 }
2873
2874                 if ( ! empty($groupby) )
2875                         $groupby = 'GROUP BY ' . $groupby;
2876                 if ( !empty( $orderby ) )
2877                         $orderby = 'ORDER BY ' . $orderby;
2878
2879                 $found_rows = '';
2880                 if ( !$q['no_found_rows'] && !empty($limits) )
2881                         $found_rows = 'SQL_CALC_FOUND_ROWS';
2882
2883                 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2884
2885                 if ( !$q['suppress_filters'] ) {
2886                         $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2887                 }
2888
2889                 if ( 'ids' == $q['fields'] ) {
2890                         $this->posts = $wpdb->get_col( $this->request );
2891                         $this->post_count = count( $this->posts );
2892                         $this->set_found_posts( $q, $limits );
2893
2894                         return $this->posts;
2895                 }
2896
2897                 if ( 'id=>parent' == $q['fields'] ) {
2898                         $this->posts = $wpdb->get_results( $this->request );
2899                         $this->post_count = count( $this->posts );
2900                         $this->set_found_posts( $q, $limits );
2901
2902                         $r = array();
2903                         foreach ( $this->posts as $post )
2904                                 $r[ $post->ID ] = $post->post_parent;
2905
2906                         return $r;
2907                 }
2908
2909                 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2910                 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2911
2912                 if ( $split_the_query ) {
2913                         // First get the IDs and then fill in the objects
2914
2915                         $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2916
2917                         $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2918
2919                         $ids = $wpdb->get_col( $this->request );
2920
2921                         if ( $ids ) {
2922                                 $this->posts = $ids;
2923                                 $this->set_found_posts( $q, $limits );
2924                                 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2925                         } else {
2926                                 $this->posts = array();
2927                         }
2928                 } else {
2929                         $this->posts = $wpdb->get_results( $this->request );
2930                         $this->set_found_posts( $q, $limits );
2931                 }
2932
2933                 // Convert to WP_Post objects
2934                 if ( $this->posts )
2935                         $this->posts = array_map( 'get_post', $this->posts );
2936
2937                 // Raw results filter. Prior to status checks.
2938                 if ( !$q['suppress_filters'] )
2939                         $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2940
2941                 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2942                         $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2943                         $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2944                         $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2945                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2946                         $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2947                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2948                         $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2949                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2950                         $this->comments = $wpdb->get_results($comments_request);
2951                         $this->comment_count = count($this->comments);
2952                 }
2953
2954                 // Check post status to determine if post should be displayed.
2955                 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2956                         $status = get_post_status($this->posts[0]);
2957                         $post_status_obj = get_post_status_object($status);
2958                         //$type = get_post_type($this->posts[0]);
2959                         if ( !$post_status_obj->public ) {
2960                                 if ( ! is_user_logged_in() ) {
2961                                         // User must be logged in to view unpublished posts.
2962                                         $this->posts = array();
2963                                 } else {
2964                                         if  ( $post_status_obj->protected ) {
2965                                                 // User must have edit permissions on the draft to preview.
2966                                                 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2967                                                         $this->posts = array();
2968                                                 } else {
2969                                                         $this->is_preview = true;
2970                                                         if ( 'future' != $status )
2971                                                                 $this->posts[0]->post_date = current_time('mysql');
2972                                                 }
2973                                         } elseif ( $post_status_obj->private ) {
2974                                                 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2975                                                         $this->posts = array();
2976                                         } else {
2977                                                 $this->posts = array();
2978                                         }
2979                                 }
2980                         }
2981
2982                         if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2983                                 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
2984                 }
2985
2986                 // Put sticky posts at the top of the posts array
2987                 $sticky_posts = get_option('sticky_posts');
2988                 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2989                         $num_posts = count($this->posts);
2990                         $sticky_offset = 0;
2991                         // Loop over posts and relocate stickies to the front.
2992                         for ( $i = 0; $i < $num_posts; $i++ ) {
2993                                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2994                                         $sticky_post = $this->posts[$i];
2995                                         // Remove sticky from current position
2996                                         array_splice($this->posts, $i, 1);
2997                                         // Move to front, after other stickies
2998                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2999                                         // Increment the sticky offset. The next sticky will be placed at this offset.
3000                                         $sticky_offset++;
3001                                         // Remove post from sticky posts array
3002                                         $offset = array_search($sticky_post->ID, $sticky_posts);
3003                                         unset( $sticky_posts[$offset] );
3004                                 }
3005                         }
3006
3007                         // If any posts have been excluded specifically, Ignore those that are sticky.
3008                         if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3009                                 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3010
3011                         // Fetch sticky posts that weren't in the query results
3012                         if ( !empty($sticky_posts) ) {
3013                                 $stickies = get_posts( array(
3014                                         'post__in' => $sticky_posts,
3015                                         'post_type' => $post_type,
3016                                         'post_status' => 'publish',
3017                                         'nopaging' => true
3018                                 ) );
3019
3020                                 foreach ( $stickies as $sticky_post ) {
3021                                         array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3022                                         $sticky_offset++;
3023                                 }
3024                         }
3025                 }
3026
3027                 if ( !$q['suppress_filters'] )
3028                         $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
3029
3030                 // Ensure that any posts added/modified via one of the filters above are
3031                 // of the type WP_Post and are filtered.
3032                 if ( $this->posts ) {
3033                         $this->post_count = count( $this->posts );
3034
3035                         $this->posts = array_map( 'get_post', $this->posts );
3036
3037                         if ( $q['cache_results'] )
3038                                 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3039
3040                         $this->post = reset( $this->posts );
3041                 } else {
3042                         $this->post_count = 0;
3043                         $this->posts = array();
3044                 }
3045
3046                 return $this->posts;
3047         }
3048
3049         /**
3050          * Set up the amount of found posts and the number of pages (if limit clause was used)
3051          * for the current query.
3052          *
3053          * @since 3.5.0
3054          * @access private
3055          */
3056         function set_found_posts( $q, $limits ) {
3057                 global $wpdb;
3058
3059                 // Bail if posts is an empty array. Continue if posts is an empty string,
3060                 // null, or false to accommodate caching plugins that fill posts later.
3061                 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3062                         return;
3063
3064                 if ( ! empty( $limits ) )
3065                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3066                 else
3067                         $this->found_posts = count( $this->posts );
3068
3069                 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3070
3071                 if ( ! empty( $limits ) )
3072                         $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3073         }
3074
3075         /**
3076          * Set up the next post and iterate current post index.
3077          *
3078          * @since 1.5.0
3079          * @access public
3080          *
3081          * @return WP_Post Next post.
3082          */
3083         function next_post() {
3084
3085                 $this->current_post++;
3086
3087                 $this->post = $this->posts[$this->current_post];
3088                 return $this->post;
3089         }
3090
3091         /**
3092          * Sets up the current post.
3093          *
3094          * Retrieves the next post, sets up the post, sets the 'in the loop'
3095          * property to true.
3096          *
3097          * @since 1.5.0
3098          * @access public
3099          * @uses $post
3100          * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3101          */
3102         function the_post() {
3103                 global $post;
3104                 $this->in_the_loop = true;
3105
3106                 if ( $this->current_post == -1 ) // loop has just started
3107                         do_action_ref_array('loop_start', array(&$this));
3108
3109                 $post = $this->next_post();
3110                 setup_postdata($post);
3111         }
3112
3113         /**
3114          * Whether there are more posts available in the loop.
3115          *
3116          * Calls action 'loop_end', when the loop is complete.
3117          *
3118          * @since 1.5.0
3119          * @access public
3120          * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3121          *
3122          * @return bool True if posts are available, false if end of loop.
3123          */
3124         function have_posts() {
3125                 if ( $this->current_post + 1 < $this->post_count ) {
3126                         return true;
3127                 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3128                         do_action_ref_array('loop_end', array(&$this));
3129                         // Do some cleaning up after the loop
3130                         $this->rewind_posts();
3131                 }
3132
3133                 $this->in_the_loop = false;
3134                 return false;
3135         }
3136
3137         /**
3138          * Rewind the posts and reset post index.
3139          *
3140          * @since 1.5.0
3141          * @access public
3142          */
3143         function rewind_posts() {
3144                 $this->current_post = -1;
3145                 if ( $this->post_count > 0 ) {
3146                         $this->post = $this->posts[0];
3147                 }
3148         }
3149
3150         /**
3151          * Iterate current comment index and return comment object.
3152          *
3153          * @since 2.2.0
3154          * @access public
3155          *
3156          * @return object Comment object.
3157          */
3158         function next_comment() {
3159                 $this->current_comment++;
3160
3161                 $this->comment = $this->comments[$this->current_comment];
3162                 return $this->comment;
3163         }
3164
3165         /**
3166          * Sets up the current comment.
3167          *
3168          * @since 2.2.0
3169          * @access public
3170          * @global object $comment Current comment.
3171          * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3172          */
3173         function the_comment() {
3174                 global $comment;
3175
3176                 $comment = $this->next_comment();
3177
3178                 if ( $this->current_comment == 0 ) {
3179                         do_action('comment_loop_start');
3180                 }
3181         }
3182
3183         /**
3184          * Whether there are more comments available.
3185          *
3186          * Automatically rewinds comments when finished.
3187          *
3188          * @since 2.2.0
3189          * @access public
3190          *
3191          * @return bool True, if more comments. False, if no more posts.
3192          */
3193         function have_comments() {
3194                 if ( $this->current_comment + 1 < $this->comment_count ) {
3195                         return true;
3196                 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3197                         $this->rewind_comments();
3198                 }
3199
3200                 return false;
3201         }
3202
3203         /**
3204          * Rewind the comments, resets the comment index and comment to first.
3205          *
3206          * @since 2.2.0
3207          * @access public
3208          */
3209         function rewind_comments() {
3210                 $this->current_comment = -1;
3211                 if ( $this->comment_count > 0 ) {
3212                         $this->comment = $this->comments[0];
3213                 }
3214         }
3215
3216         /**
3217          * Sets up the WordPress query by parsing query string.
3218          *
3219          * @since 1.5.0
3220          * @access public
3221          *
3222          * @param string $query URL query string.
3223          * @return array List of posts.
3224          */
3225         function query( $query ) {
3226                 $this->init();
3227                 $this->query = $this->query_vars = wp_parse_args( $query );
3228                 return $this->get_posts();
3229         }
3230
3231         /**
3232          * Retrieve queried object.
3233          *
3234          * If queried object is not set, then the queried object will be set from
3235          * the category, tag, taxonomy, posts page, single post, page, or author
3236          * query variable. After it is set up, it will be returned.
3237          *
3238          * @since 1.5.0
3239          * @access public
3240          *
3241          * @return object
3242          */
3243         function get_queried_object() {
3244                 if ( isset($this->queried_object) )
3245                         return $this->queried_object;
3246
3247                 $this->queried_object = null;
3248                 $this->queried_object_id = 0;
3249
3250                 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3251                         $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3252
3253                         $query = reset( $tax_query_in_and );
3254
3255                         if ( 'term_id' == $query['field'] )
3256                                 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3257                         elseif ( $query['terms'] )
3258                                 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3259
3260                         if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
3261                                 $this->queried_object = $term;
3262                                 $this->queried_object_id = (int) $term->term_id;
3263
3264                                 if ( $this->is_category )
3265                                         _make_cat_compat( $this->queried_object );
3266                         }
3267                 } elseif ( $this->is_post_type_archive ) {
3268                         $post_type = $this->get( 'post_type' );
3269                         if ( is_array( $post_type ) )
3270                                 $post_type = reset( $post_type );
3271                         $this->queried_object = get_post_type_object( $post_type );
3272                 } elseif ( $this->is_posts_page ) {
3273                         $page_for_posts = get_option('page_for_posts');
3274                         $this->queried_object = get_post( $page_for_posts );
3275                         $this->queried_object_id = (int) $this->queried_object->ID;
3276                 } elseif ( $this->is_singular && !is_null($this->post) ) {
3277                         $this->queried_object = $this->post;
3278                         $this->queried_object_id = (int) $this->post->ID;
3279                 } elseif ( $this->is_author ) {
3280                         $this->queried_object_id = (int) $this->get('author');
3281                         $this->queried_object = get_userdata( $this->queried_object_id );
3282                 }
3283
3284                 return $this->queried_object;
3285         }
3286
3287         /**
3288          * Retrieve ID of the current queried object.
3289          *
3290          * @since 1.5.0
3291          * @access public
3292          *
3293          * @return int
3294          */
3295         function get_queried_object_id() {
3296                 $this->get_queried_object();
3297
3298                 if ( isset($this->queried_object_id) ) {
3299                         return $this->queried_object_id;
3300                 }
3301
3302                 return 0;
3303         }
3304
3305         /**
3306          * Constructor.
3307          *
3308          * Sets up the WordPress query, if parameter is not empty.
3309          *
3310          * @since 1.5.0
3311          * @access public
3312          *
3313          * @param string $query URL query string.
3314          * @return WP_Query
3315          */
3316         function __construct($query = '') {
3317                 if ( ! empty($query) ) {
3318                         $this->query($query);
3319                 }
3320         }
3321
3322         /**
3323          * Is the query for an existing archive page?
3324          *
3325          * Month, Year, Category, Author, Post Type archive...
3326          *
3327          * @since 3.1.0
3328          *
3329          * @return bool
3330          */
3331         function is_archive() {
3332                 return (bool) $this->is_archive;
3333         }
3334
3335         /**
3336          * Is the query for an existing post type archive page?
3337          *
3338          * @since 3.1.0
3339          *
3340          * @param mixed $post_types Optional. Post type or array of posts types to check against.
3341          * @return bool
3342          */
3343         function is_post_type_archive( $post_types = '' ) {
3344                 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3345                         return (bool) $this->is_post_type_archive;
3346
3347                 $post_type = $this->get( 'post_type' );
3348                 if ( is_array( $post_type ) )
3349                         $post_type = reset( $post_type );
3350                 $post_type_object = get_post_type_object( $post_type );
3351
3352                 return in_array( $post_type_object->name, (array) $post_types );
3353         }
3354
3355         /**
3356          * Is the query for an existing attachment page?
3357          *
3358          * @since 3.1.0
3359          *
3360          * @return bool
3361          */
3362         function is_attachment() {
3363                 return (bool) $this->is_attachment;
3364         }
3365
3366         /**
3367          * Is the query for an existing author archive page?
3368          *
3369          * If the $author parameter is specified, this function will additionally
3370          * check if the query is for one of the authors specified.
3371          *
3372          * @since 3.1.0
3373          *
3374          * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3375          * @return bool
3376          */
3377         function is_author( $author = '' ) {
3378                 if ( !$this->is_author )
3379                         return false;
3380
3381                 if ( empty($author) )
3382                         return true;
3383
3384                 $author_obj = $this->get_queried_object();
3385
3386                 $author = (array) $author;
3387
3388                 if ( in_array( $author_obj->ID, $author ) )
3389                         return true;
3390                 elseif ( in_array( $author_obj->nickname, $author ) )
3391                         return true;
3392                 elseif ( in_array( $author_obj->user_nicename, $author ) )
3393                         return true;
3394
3395                 return false;
3396         }
3397
3398         /**
3399          * Is the query for an existing category archive page?
3400          *
3401          * If the $category parameter is specified, this function will additionally
3402          * check if the query is for one of the categories specified.
3403          *
3404          * @since 3.1.0
3405          *
3406          * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3407          * @return bool
3408          */
3409         function is_category( $category = '' ) {
3410                 if ( !$this->is_category )
3411                         return false;
3412
3413                 if ( empty($category) )
3414                         return true;
3415
3416                 $cat_obj = $this->get_queried_object();
3417
3418                 $category = (array) $category;
3419
3420                 if ( in_array( $cat_obj->term_id, $category ) )
3421                         return true;
3422                 elseif ( in_array( $cat_obj->name, $category ) )
3423                         return true;
3424                 elseif ( in_array( $cat_obj->slug, $category ) )
3425                         return true;
3426
3427                 return false;
3428         }
3429
3430         /**
3431          * Is the query for an existing tag archive page?
3432          *
3433          * If the $tag parameter is specified, this function will additionally
3434          * check if the query is for one of the tags specified.
3435          *
3436          * @since 3.1.0
3437          *
3438          * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3439          * @return bool
3440          */
3441         function is_tag( $tag = '' ) {
3442                 if ( ! $this->is_tag )
3443                         return false;
3444
3445                 if ( empty( $tag ) )
3446                         return true;
3447
3448                 $tag_obj = $this->get_queried_object();
3449
3450                 $tag = (array) $tag;
3451
3452                 if ( in_array( $tag_obj->term_id, $tag ) )
3453                         return true;
3454                 elseif ( in_array( $tag_obj->name, $tag ) )
3455                         return true;
3456                 elseif ( in_array( $tag_obj->slug, $tag ) )
3457                         return true;
3458
3459                 return false;
3460         }
3461
3462         /**
3463          * Is the query for an existing taxonomy archive page?
3464          *
3465          * If the $taxonomy parameter is specified, this function will additionally
3466          * check if the query is for that specific $taxonomy.
3467          *
3468          * If the $term parameter is specified in addition to the $taxonomy parameter,
3469          * this function will additionally check if the query is for one of the terms
3470          * specified.
3471          *
3472          * @since 3.1.0
3473          *
3474          * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3475          * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3476          * @return bool
3477          */
3478         function is_tax( $taxonomy = '', $term = '' ) {
3479                 global $wp_taxonomies;
3480
3481                 if ( !$this->is_tax )
3482                         return false;
3483
3484                 if ( empty( $taxonomy ) )
3485                         return true;
3486
3487                 $queried_object = $this->get_queried_object();
3488                 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3489                 $term_array = (array) $term;
3490
3491                 // Check that the taxonomy matches.
3492                 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3493                         return false;
3494
3495                 // Only a Taxonomy provided.
3496                 if ( empty( $term ) )
3497                         return true;
3498
3499                 return isset( $queried_object->term_id ) &&
3500                         count( array_intersect(
3501                                 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3502                                 $term_array
3503                         ) );
3504         }
3505
3506         /**
3507          * Whether the current URL is within the comments popup window.
3508          *
3509          * @since 3.1.0
3510          *
3511          * @return bool
3512          */
3513         function is_comments_popup() {
3514                 return (bool) $this->is_comments_popup;
3515         }
3516
3517         /**
3518          * Is the query for an existing date archive?
3519          *
3520          * @since 3.1.0
3521          *
3522          * @return bool
3523          */
3524         function is_date() {
3525                 return (bool) $this->is_date;
3526         }
3527
3528         /**
3529          * Is the query for an existing day archive?
3530          *
3531          * @since 3.1.0
3532          *
3533          * @return bool
3534          */
3535         function is_day() {
3536                 return (bool) $this->is_day;
3537         }
3538
3539         /**
3540          * Is the query for a feed?
3541          *
3542          * @since 3.1.0
3543          *
3544          * @param string|array $feeds Optional feed types to check.
3545          * @return bool
3546          */
3547         function is_feed( $feeds = '' ) {
3548                 if ( empty( $feeds ) || ! $this->is_feed )
3549                         return (bool) $this->is_feed;
3550                 $qv = $this->get( 'feed' );
3551                 if ( 'feed' == $qv )
3552                         $qv = get_default_feed();
3553                 return in_array( $qv, (array) $feeds );
3554         }
3555
3556         /**
3557          * Is the query for a comments feed?
3558          *
3559          * @since 3.1.0
3560          *
3561          * @return bool
3562          */
3563         function is_comment_feed() {
3564                 return (bool) $this->is_comment_feed;
3565         }
3566
3567         /**
3568          * Is the query for the front page of the site?
3569          *
3570          * This is for what is displayed at your site's main URL.
3571          *
3572          * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3573          *
3574          * If you set a static page for the front page of your site, this function will return
3575          * true when viewing that page.
3576          *
3577          * Otherwise the same as @see WP_Query::is_home()
3578          *
3579          * @since 3.1.0
3580          * @uses is_home()
3581          * @uses get_option()
3582          *
3583          * @return bool True, if front of site.
3584          */
3585         function is_front_page() {
3586                 // most likely case
3587                 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3588                         return true;
3589                 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3590                         return true;
3591                 else
3592                         return false;
3593         }
3594
3595         /**
3596          * Is the query for the blog homepage?
3597          *
3598          * This is the page which shows the time based blog content of your site.
3599          *
3600          * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3601          *
3602          * If you set a static page for the front page of your site, this function will return
3603          * true only on the page you set as the "Posts page".
3604          *
3605          * @see WP_Query::is_front_page()
3606          *
3607          * @since 3.1.0
3608          *
3609          * @return bool True if blog view homepage.
3610          */
3611         function is_home() {
3612                 return (bool) $this->is_home;
3613         }
3614
3615         /**
3616          * Is the query for an existing month archive?
3617          *
3618          * @since 3.1.0
3619          *
3620          * @return bool
3621          */
3622         function is_month() {
3623                 return (bool) $this->is_month;
3624         }
3625
3626         /**
3627          * Is the query for an existing single page?
3628          *
3629          * If the $page parameter is specified, this function will additionally
3630          * check if the query is for one of the pages specified.
3631          *
3632          * @see WP_Query::is_single()
3633          * @see WP_Query::is_singular()
3634          *
3635          * @since 3.1.0
3636          *
3637          * @param mixed $page Page ID, title, slug, or array of such.
3638          * @return bool
3639          */
3640         function is_page( $page = '' ) {
3641                 if ( !$this->is_page )
3642                         return false;
3643
3644                 if ( empty( $page ) )
3645                         return true;
3646
3647                 $page_obj = $this->get_queried_object();
3648
3649                 $page = (array) $page;
3650
3651                 if ( in_array( $page_obj->ID, $page ) )
3652                         return true;
3653                 elseif ( in_array( $page_obj->post_title, $page ) )
3654                         return true;
3655                 else if ( in_array( $page_obj->post_name, $page ) )
3656                         return true;
3657
3658                 return false;
3659         }
3660
3661         /**
3662          * Is the query for paged result and not for the first page?
3663          *
3664          * @since 3.1.0
3665          *
3666          * @return bool
3667          */
3668         function is_paged() {
3669                 return (bool) $this->is_paged;
3670         }
3671
3672         /**
3673          * Is the query for a post or page preview?
3674          *
3675          * @since 3.1.0
3676          *
3677          * @return bool
3678          */
3679         function is_preview() {
3680                 return (bool) $this->is_preview;
3681         }
3682
3683         /**
3684          * Is the query for the robots file?
3685          *
3686          * @since 3.1.0
3687          *
3688          * @return bool
3689          */
3690         function is_robots() {
3691                 return (bool) $this->is_robots;
3692         }
3693
3694         /**
3695          * Is the query for a search?
3696          *
3697          * @since 3.1.0
3698          *
3699          * @return bool
3700          */
3701         function is_search() {
3702                 return (bool) $this->is_search;
3703         }
3704
3705         /**
3706          * Is the query for an existing single post?
3707          *
3708          * Works for any post type, except attachments and pages
3709          *
3710          * If the $post parameter is specified, this function will additionally
3711          * check if the query is for one of the Posts specified.
3712          *
3713          * @see WP_Query::is_page()
3714          * @see WP_Query::is_singular()
3715          *
3716          * @since 3.1.0
3717          *
3718          * @param mixed $post Post ID, title, slug, or array of such.
3719          * @return bool
3720          */
3721         function is_single( $post = '' ) {
3722                 if ( !$this->is_single )
3723                         return false;
3724
3725                 if ( empty($post) )
3726                         return true;
3727
3728                 $post_obj = $this->get_queried_object();
3729
3730                 $post = (array) $post;
3731
3732                 if ( in_array( $post_obj->ID, $post ) )
3733                         return true;
3734                 elseif ( in_array( $post_obj->post_title, $post ) )
3735                         return true;
3736                 elseif ( in_array( $post_obj->post_name, $post ) )
3737                         return true;
3738
3739                 return false;
3740         }
3741
3742         /**
3743          * Is the query for an existing single post of any post type (post, attachment, page, ... )?
3744          *
3745          * If the $post_types parameter is specified, this function will additionally
3746          * check if the query is for one of the Posts Types specified.
3747          *
3748          * @see WP_Query::is_page()
3749          * @see WP_Query::is_single()
3750          *
3751          * @since 3.1.0
3752          *
3753          * @param mixed $post_types Optional. Post Type or array of Post Types
3754          * @return bool
3755          */
3756         function is_singular( $post_types = '' ) {
3757                 if ( empty( $post_types ) || !$this->is_singular )
3758                         return (bool) $this->is_singular;
3759
3760                 $post_obj = $this->get_queried_object();
3761
3762                 return in_array( $post_obj->post_type, (array) $post_types );
3763         }
3764
3765         /**
3766          * Is the query for a specific time?
3767          *
3768          * @since 3.1.0
3769          *
3770          * @return bool
3771          */
3772         function is_time() {
3773                 return (bool) $this->is_time;
3774         }
3775
3776         /**
3777          * Is the query for a trackback endpoint call?
3778          *
3779          * @since 3.1.0
3780          *
3781          * @return bool
3782          */
3783         function is_trackback() {
3784                 return (bool) $this->is_trackback;
3785         }
3786
3787         /**
3788          * Is the query for an existing year archive?
3789          *
3790          * @since 3.1.0
3791          *
3792          * @return bool
3793          */
3794         function is_year() {
3795                 return (bool) $this->is_year;
3796         }
3797
3798         /**
3799          * Is the query a 404 (returns no results)?
3800          *
3801          * @since 3.1.0
3802          *
3803          * @return bool
3804          */
3805         function is_404() {
3806                 return (bool) $this->is_404;
3807         }
3808
3809         /**
3810          * Is the query the main query?
3811          *
3812          * @since 3.3.0
3813          *
3814          * @return bool
3815          */
3816         function is_main_query() {
3817                 global $wp_the_query;
3818                 return $wp_the_query === $this;
3819         }
3820
3821         /**
3822          * After looping through a nested query, this function
3823          * restores the $post global to the current post in this query.
3824          *
3825          * @since 3.7.0
3826          *
3827          * @return bool
3828          */
3829         function reset_postdata() {
3830                 if ( ! empty( $this->post ) ) {
3831                         $GLOBALS['post'] = $this->post;
3832                         setup_postdata( $this->post );
3833                 }
3834         }
3835 }
3836
3837 /**
3838  * Redirect old slugs to the correct permalink.
3839  *
3840  * Attempts to find the current slug from the past slugs.
3841  *
3842  * @since 2.1.0
3843  * @uses $wp_query
3844  * @uses $wpdb
3845  *
3846  * @return null If no link is found, null is returned.
3847  */
3848 function wp_old_slug_redirect() {
3849         global $wp_query;
3850         if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3851                 global $wpdb;
3852
3853                 // Guess the current post_type based on the query vars.
3854                 if ( get_query_var('post_type') )
3855                         $post_type = get_query_var('post_type');
3856                 elseif ( !empty($wp_query->query_vars['pagename']) )
3857                         $post_type = 'page';
3858                 else
3859                         $post_type = 'post';
3860
3861                 if ( is_array( $post_type ) ) {
3862                         if ( count( $post_type ) > 1 )
3863                                 return;
3864                         $post_type = array_shift( $post_type );
3865                 }
3866
3867                 // Do not attempt redirect for hierarchical post types
3868                 if ( is_post_type_hierarchical( $post_type ) )
3869                         return;
3870
3871                 $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
3872
3873                 // if year, monthnum, or day have been specified, make our query more precise
3874                 // just in case there are multiple identical _wp_old_slug values
3875                 if ( '' != $wp_query->query_vars['year'] )
3876                         $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3877                 if ( '' != $wp_query->query_vars['monthnum'] )
3878                         $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3879                 if ( '' != $wp_query->query_vars['day'] )
3880                         $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3881
3882                 $id = (int) $wpdb->get_var($query);
3883
3884                 if ( ! $id )
3885                         return;
3886
3887                 $link = get_permalink($id);
3888
3889                 if ( !$link )
3890                         return;
3891
3892                 wp_redirect( $link, 301 ); // Permanent redirect
3893                 exit;
3894         endif;
3895 }
3896
3897 /**
3898  * Set up global post data.
3899  *
3900  * @since 1.5.0
3901  *
3902  * @param object $post Post data.
3903  * @uses do_action_ref_array() Calls 'the_post'
3904  * @return bool True when finished.
3905  */
3906 function setup_postdata( $post ) {
3907         global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3908
3909         $id = (int) $post->ID;
3910
3911         $authordata = get_userdata($post->post_author);
3912
3913         $currentday = mysql2date('d.m.y', $post->post_date, false);
3914         $currentmonth = mysql2date('m', $post->post_date, false);
3915         $numpages = 1;
3916         $multipage = 0;
3917         $page = get_query_var('page');
3918         if ( ! $page )
3919                 $page = 1;
3920         if ( is_single() || is_page() || is_feed() )
3921                 $more = 1;
3922         $content = $post->post_content;
3923         if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
3924                 if ( $page > 1 )
3925                         $more = 1;
3926                 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
3927                 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
3928                 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
3929                 // Ignore nextpage at the beginning of the content.
3930                 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
3931                         $content = substr( $content, 15 );
3932                 $pages = explode('<!--nextpage-->', $content);
3933                 $numpages = count($pages);
3934                 if ( $numpages > 1 )
3935                         $multipage = 1;
3936         } else {
3937                 $pages = array( $post->post_content );
3938         }
3939
3940         do_action_ref_array('the_post', array(&$post));
3941
3942         return true;
3943 }