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